[英]Using variable with php from javascript onsubmit event
I'm trying to use a value from onsubmit() in php. 我正在尝试使用php中onsubmit()的值。
More specific: An HTML form is being submitted by the client. 更具体:客户端正在提交HTML表单。 The onSubmit passes it to a couple of javascript functions. onSubmit将其传递给两个javascript函数。 One of these functions is supposed to fetch a list of registered emails from the database to check if it is already used. 这些功能之一应该从数据库中获取已注册电子邮件的列表,以检查其是否已被使用。
<?php
$email='?> frm.add_fd2.value <?php ';
// execute query
$num_rows = mysql_num_rows($result); ?>
var num_rows = <?php echo (is_int($num_rows)) ? $num_rows : 0; ?>;
if (num_rows > 0) {
// JAVASCRIPT ERROR
}
JavaScript is a client-side language. JavaScript是一种客户端语言。 It looks like you're trying to retrieve a value from it through PHP, which will never happen. 看来您正在尝试通过PHP从中检索值,但这种情况永远不会发生。 PHP will be long-executed before JavaScript even comes in to play. PHP将在JavaScript发挥作用之前长期执行。
You need to either submit the value to PHP through a form, or use something like an AJAX call and send PHP the value so it can process it after the page has been loaded. 您需要通过表单将值提交给PHP,或者使用类似AJAX的调用并将其发送给PHP,以便在页面加载后对其进行处理。
In your case, AJAX appears to be a better option. 就您而言,AJAX似乎是一个更好的选择。 In which case something like the following should work: 在这种情况下,应执行以下操作:
$.getJSON('verify_email.php',{email:frm.add_fd2.value},function(data){
if (data.matches>0){
// matches were found
}else{
// no matches found
}
});
then in verify_email.php: 然后在verify_email.php中:
<?php
// ... establish connection ...
$email = mysql_real_escape_string($_GET['email']); // note, this should be checked for existance
$result = mysql_query("SELECT * FROM table WHERE email='{$email}'");
header('Content-Type: application/json');
echo '{matches:'.mysql_num_rows($result).'}';
?>
Or something of that sort. 或类似的东西。 (Note I'm using jQuery to make this syntactically easier). (请注意,我正在使用jQuery来简化语法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.