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??
}
4

4 回答 4

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');});

}
于 2013-06-14T08:36:57.110 回答
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!

于 2013-06-14T08:29:46.250 回答
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
}
于 2013-06-14T08:34:36.360 回答
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;
    }
}
于 2013-06-14T08:47:18.187 回答