1

Hello I am writing a PHP application and I am struck when I need to check the form input from data base

<form id="form1" name="form1" method="post">
<input type="text" id="acname" name="acname"/>
<button name="save" type="submit" onclick="return checkform()">Save</button>
</form>

Javascript function

<SCRIPT LANGUAGE="JavaScript">
function checkform()
{
   if ($.trim($("#acname").val()).length == 0){
        alert("Please Enter Name");
        $("#acname").focus();
        return false;
    } 

//Here I need to check the name is already in the database or not, so I require the PHP Code to interact with database, how Can I achieve this??
}
Mandeep Singh
  • 2,016
  • 7
  • 19
  • 32
  • 3
    You should do an AJAX request to check the name. – Stephan Jun 14 '13 at 08:27
  • 1
    It's not possible to write PHP code in that context. You need to make an AJAX request instead. However, in this case doing that directly won't work because your event handler is synchronous while AJAX is not. – Jon Jun 14 '13 at 08:28
  • 1
    you can do this with ajax see this example http://stackoverflow.com/questions/5004233/jquery-ajax-post-example/14217926#14217926 – NullPoiиteя Jun 14 '13 at 08:28
  • 1
    AJAX certainly. You say nothing about the structure or type of your database, so any help will be difficult to provide. –  Jun 14 '13 at 08:30

4 Answers4

3

chnage you Javascript function

function checkform()
{
   if ($.trim($("#acname").val()) == ''){
        alert("Please Enter Name");
        $("#acname").focus();
        return false;
    } 
       $.post(<url>,{name:$("#acname").val()},function(){alert('data saved');});

}
Hitesh Siddhapura
  • 1,401
  • 15
  • 20
1

Do an in-page AJAX request with Javascript, submitting the name, and then check the name with PHP, returning a JSON object with whether the name is already taken or not.

I see you use jQuery, so check eg http://api.jquery.com/jQuery.ajax/ A simple example would be

$.ajax( "check.php?name=" + $("#acname").val() )
.done(function(data) { console.log("Return message is ", data); })
.fail(function() { console.log("Something went wrong"); });

Note that when the form is submitted, check the availability again on the server-side. Never trust what comes from the front-end!

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62
1

HTML

<input type="text" id="acname" name="acname"/>
<input type='button' id='btn_submit' value='Submit'>

<input type='text' id='result'>

AJAX

$(document).ready(function()
{
    var name = $('#acname').val();

    $.ajax({
        url: 'check_name.php',
        type: 'POST',
        data: { name: name },
        success: function(data)
        {
            $('#result').text(data);
        }
    })
});

PHP

if(isset($_POST['name']))
{
    $name = $_POST['name'];

    $sql = $this->db->query("SELECT name FROM YOUR_TABLE WHERE name ='". $name ."'");
    if($sql->num_rows() != 0)
    {
        echo "Name exists";
    }
    else
        echo "Name available!";
    // and so on
    // whatever you echo here goes with the data returned in AJAX
}
Þaw
  • 2,047
  • 4
  • 22
  • 39
1

Use an XML RPC call to talk to your database. For clarity I use a synchronous call in the example below, as indicated by the false parameter to rpc.open().

function checkform()
{
   var name = $.trim($("#acname").val());
   if (name.length == 0){
        alert("Please Enter Name");
        $("#acname").focus();
        return false;
    } 

    // Ask the database. The 'http://host/check_name_in_db.php' must be a PHP script
    // that can handle receiving an HTTP POST with a parameter named 'name'. It then
    // has to determine if the name passed in exists or not and return an answer.
    // The answer can be in any format you'd like, many people use XML or JSON for this.
    // For this example, I just assume that your PHP script will return (echo, print,
    // printf()) the strings 'yes' or 'no' as a response to whether or not the name was
    // found in the database.
    var params='name='+name;
    var rpc = new XMLHttpRequest();
    rpc.open('POST', 'http://host/check_name_in_db.php', false);
    rpc.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    rpc.send(params);

    // Wait until the RPC request is complete.
    // This is somewhat dangerous as an RPC call _can_ be aborted for example.
    // That could cause your javascript to hang here.
    while (rpc.readyState != 4) {};
    var reply = rpc.responseText;

    // Your PHP script wrote 'yes' back to us, which would mean that the name was
    // found in the database.
    if (reply == 'yes')
    {
        return true;
    }
    else
    {
        return false;
    }
}
Linus Swälas
  • 130
  • 1
  • 6