Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:53:35+00:00 2026-06-09T05:53:35+00:00

The below code is to create a campaign. Before creation, I have to validate

  • 0

The below code is to create a campaign. Before creation, I have to validate the form. I have to validate the campaign name which is already existed in database or not. I don’t know whether I can use PHP code inside javascript (like below).Anyway it’s not working. How can I change my code? How can I validate values with database values?

$this->campaign is an array which contain all campaign details from database.

<script type="text/JavaScript">
function validate()
{
var name = document.getElementById('name').value;
var shape = document.getElementById('shape').value;
     <?
     foreach($this->campaign as $c)
     {
     $old_cname=$c['name'];
     ?>
        if(name==<?=$old_cname;?>)
        {
            alert("same name exists in database. Try again!");
        }
    <?
    }
    ?>

if(!name)
{
    alert("Please enter a name!");

    return false;
}

if(!shape)
{
    alert("Please select shape!");
    return false;
}
    return true;


}
</script>


<form action="create.php" method="post" onsubmit="return(validate());">
Name:
<input type="text" name="name" id="name"/>
Shape:
<select name="shape" id="shape">
<option value="long">Long</option>
<option value="tall">Tall</option>
</select>
<input type="submit" value="Create" name="submit"/>
</form>

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:53:36+00:00Added an answer on June 9, 2026 at 5:53 am

    You can’t mix php and javascript like that.. php is a server-side language, while javascript is client-side; php renders the page before the user sees it, while javascript modifies the page without refreshing. Any php values in your js will get rendered and output before the js even executes.

    In order to do what you need, you need to use ajax, which is asynchronous javascript and xml, a method of client-server communication that allows for what you want to happen.

    To do this, I would suggest jQuery, a javascript library which makes such requests very simple. As an example of how you would make such a request in jquery….

    The jQuery.ajax() method:

    $.ajax({
        url: "validate.php",
        type: "POST",
        data: "username=" + username,
        sucesss: function(data) {
            if (data == 1)
                $("#ajax_div").html("Username is taken, choose another!");
            }
            else {
                $("#ajax_div").html("Username is free :)");
            }
        }
    });
    

    That would be how to do it in ajax, while your php file would either return a 1 or a 0 depending on the result of an sql query comparing usernames in the database.

    To do this without jquery, it would be something like this:

    function checkUsername() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4) {
                if (xmlhttp.responseText == 1) {
                    document.getElementById('ajax_div').innerHTML = "Username is taken, please choose another!";
                }
                else {
                    document.getElementById('ajax_div').innerHTML = "Username is free :)";
                }
            }
        }
        xmlhttp.open("POST","validate.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("username=" + username);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the below code to create and read my database in the app
I am using below code to to create Contact Service and to Validate login
i expected below code to create table which has BIGSERIAL column type id. @Entity
I have this code(below) to create a customizable Hot Key. OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent,void
I have the below code to create a class in javascript.... Now i will
The code below create one circle inside the windows form. This code compiled without
The below code which uses npoi to create excel documents displays images in open
I have the code below to create a XML file <?php try { $dom
I am using below code to create a reminder in Google calendar (using Google
the code below gives compilation error when I try to create test t[2]; because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.