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??
}
-
3You should do an AJAX request to check the name.Stephan– Stephan2013年06月14日 08:27:38 +00:00Commented Jun 14, 2013 at 8:27
-
1It'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– Jon2013年06月14日 08:28:16 +00:00Commented Jun 14, 2013 at 8:28
-
1you can do this with ajax see this example stackoverflow.com/questions/5004233/jquery-ajax-post-example/…NullPoiиteя– NullPoiиteя2013年06月14日 08:28:27 +00:00Commented Jun 14, 2013 at 8:28
-
1AJAX certainly. You say nothing about the structure or type of your database, so any help will be difficult to provide.user1864610– user18646102013年06月14日 08:30:08 +00:00Commented Jun 14, 2013 at 8:30
4 Answers 4
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');});
}
Comments
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!
1 Comment
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
}
Comments
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;
}
}