javascript函数中的php
您好,我正在编写一个PHP应用程序,当我需要检查来自数据库的表单输入时,我感到震惊javascript函数中的php,php,javascript,Php,Javascript,您好,我正在编写一个PHP应用程序,当我需要检查来自数据库的表单输入时,我感到震惊 <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>
<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函数
<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??
}
function checkform()
{
if ($.trim($("#acname").val()) == ''){
alert("Please Enter Name");
$("#acname").focus();
return false;
}
$.post(<url>,{name:$("#acname").val()},function(){alert('data saved');});
}
使用Javascript执行页面内AJAX请求,提交名称,然后使用PHP检查名称,返回一个JSON对象,说明名称是否已被使用 我看到您使用jQuery,所以请检查 一个简单的例子是
$.ajax( "check.php?name=" + $("#acname").val() )
.done(function(data) { console.log("Return message is ", data); })
.fail(function() { console.log("Something went wrong"); });
请注意,提交表单时,请再次检查服务器端的可用性。永远不要相信来自前端的东西 使用Javascript执行页面内AJAX请求,提交名称,然后使用PHP检查名称,返回一个JSON对象,说明名称是否已被使用 我看到您使用jQuery,所以请检查 一个简单的例子是
$.ajax( "check.php?name=" + $("#acname").val() )
.done(function(data) { console.log("Return message is ", data); })
.fail(function() { console.log("Something went wrong"); });
请注意,提交表单时,请再次检查服务器端的可用性。永远不要相信来自前端的东西 HTML
PHP
HTML
PHP
如何使用Javascript函数
<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??
}
function checkform()
{
if ($.trim($("#acname").val()) == ''){
alert("Please Enter Name");
$("#acname").focus();
return false;
}
$.post(<url>,{name:$("#acname").val()},function(){alert('data saved');});
}
如何使用Javascript函数
<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??
}
function checkform()
{
if ($.trim($("#acname").val()) == ''){
alert("Please Enter Name");
$("#acname").focus();
return false;
}
$.post(<url>,{name:$("#acname").val()},function(){alert('data saved');});
}
使用XML RPC调用与数据库对话。 为了清楚起见,我在下面的示例中使用了同步调用,如rpc.open的false参数所示
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;
}
}
使用XML RPC调用与数据库对话。 为了清楚起见,我在下面的示例中使用了同步调用,如rpc.open的false参数所示
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;
}
}
您应该执行一个AJAX请求来检查名称。在该上下文中编写PHP代码是不可能的。您需要发出AJAX请求。但是,在这种情况下,直接这样做是不起作用的,因为事件处理程序是同步的,而AJAX不是同步的。您对数据库的结构或类型只字未提,因此很难提供任何帮助。您应该执行AJAX请求以检查名称。在该上下文中编写PHP代码是不可能的。您需要发出AJAX请求。但是,在这种情况下,直接这样做是不起作用的,因为事件处理程序是同步的,而AJAX不是同步的。您对数据库的结构或类型只字未提,因此很难提供任何帮助。请打开一个新问题,并给出确切的错误消息,以及您已经尝试过的内容,我将尝试查看它!请打开一个带有确切错误消息的新问题,以及您已经尝试过的内容,我将尝试查看它!