我有一个表格,询问新用户的电子邮件。我有一个php函数,可以用这封电子邮件获取信息(名字,姓氏,办公室,工作.)使用cURL请求并将其返回到数组($full_informations)中。
我希望在电子邮件输入后执行这个函数。由于不同的原因,我不能直接将代码添加到我的表单中,所以我需要一些东西可以在身体或头部的其他地方读取。
我得到了一个由脚本自动填充的字段:
<input onKeyPress="" class="editingSize " type="text" id="emails" name="emails" size="" value="" maxlength="150">我希望能够将此字段的值发送到php函数,如
$full_informations = get_more_info_from_mail($email)然后我就可以做一些事情
$firstname = $full_informations['firstname'];
$lastname = $full_informations['lastname'];
$job = $full_informations['job'];
//...并自动将这些变量插入mysql中,而不要求用户完成表单(我知道如何完成该部分)。
所以,我的问题是,如何在用户输入电子邮件之后,用字段的值调用我的函数?
我想我需要一些ajax请求,但我根本不熟悉这些请求。
发布于 2013-03-31 20:01:26
使用jquery:
<script type="text/javascript">
$(function() {
$('#emails').on('blur',function() {
$.post('/path/to/your/php.php',
{email: $('#emails').val()},
function(json) {
alert('responded with '+json.response);
});
});
});
</script>您的php脚本应该如下所示:
<?php
$email = $_GET['email'];
// make sure you verify the email is in a correct format.
// save it to a database
//if it succeeds:
echo json_encode(array('response'=>'success'));
//if it fails:
echo json_encode(array('response'=>'failure'));发布于 2013-03-31 20:09:34
你在使用mysql数据库吗。您可以在php中实现如下所示:
把这个放在你页面的开头
<?
if (isset($_POST['submit'])) {
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$mail = $_POST['mail'];
$result = mysql_query("SELECT * FROM users WHERE email = $mail");
$row = mysql_fetch_row($result);
$firstName = $row[0]; //FirstName
$lastName = $row[1]; //LastName
$job = $row[2]; //Job
}
?>您的html应该如下所示:
<form action="" method="POST">
<input type="text" name="mail" />
<input type="submit" value="submit" name="submit" />
</form>希望这能有所帮助!
https://stackoverflow.com/questions/15733709
复制相似问题