Php 为什么不是';我的脚本没有向ajax函数返回值吗?
这是我第一次使用ajax。我只是想将变量传递给php处理脚本,该脚本使用ajax发送电子邮件。然而,我并没有从php脚本返回任何东西来告诉ajax它已经成功了,即使它成功了,并且我要求它返回0或1 我的ajax是:Php 为什么不是';我的脚本没有向ajax函数返回值吗?,php,ajax,Php,Ajax,这是我第一次使用ajax。我只是想将变量传递给php处理脚本,该脚本使用ajax发送电子邮件。然而,我并没有从php脚本返回任何东西来告诉ajax它已经成功了,即使它成功了,并且我要求它返回0或1 我的ajax是: jQuery.ajax({ //this is the php file that processes the data and send mail url: "process.php", //GET method is used
jQuery.ajax({
//this is the php file that processes the data and send mail
url: "process.php",
//GET method is used
type: "GET",
//pass the data
data: dataString,
//Do not cache the page
cache: false,
//success
success: function (html) {
alert(html);
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form
jQuery('.form').fadeOut('slow');
//show the success message
jQuery('.done').fadeIn('slow');
//if process.php returned 0/false (send mail failed)
} else alert('Sorry, unexpected error. Please try again later.');
}
});
我的php是:
$username=htmlspecialchars(stripslashes($_GET['username']));
$telnumber=htmlspecialchars(stripslashes($_GET['telnumber']));
$email=htmlspecialchars(stripslashes($_GET['email']));
$numberparty=htmlspecialchars(stripslashes($_GET['numberparty']));
$message=htmlspecialchars(stripslashes($_GET['message']));
$to=//myemail address;
$workshopName=htmlspecialchars(stripslashes($_GET['workshopName']));
$subject='Booking request for: '.$workshopName;
$body= "<ul><li>Name: ".$username."</li>";
$body.="<li>Email: ".$email."</li>";
$body.="<li>Telephone Number: ".$telnumber."</li>";
$body.="<li>Number in party: ".$numberparty."</li>";
if(!empty($message)){
$body.="<li>Message: ".$message."</li>";
}
$body.="</ul>";
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
$result = sendmail($to, $subject, $body, $email);
$username=htmlspecialchars(斜杠($\u GET['username']);
$telnumber=htmlspecialchars(带斜杠($\u GET['telnumber']);
$email=htmlspecialchars(斜杠($\u GET['email']);
$numberparty=htmlspecialchars(带斜杠($_GET['numberparty']);
$message=htmlspecialchars(带斜杠($\u GET['message']);
$to=//我的邮件地址;
$workshopName=htmlspecialchars(带斜杠($\u GET['workshopName']);
$subject='预订请求:'.$workshopName;
$body=“- 名称:“.$username.”
”;
$body.=“- 电子邮件:”.$Email.
”;
$body.=“- 电话号码:”.$telnumber.
”;
$body.=“- 参与方中的编号:”.$numberparty.“
”;
如果(!empty($message)){
$body.=“- 消息:“.$Message.”
”;
}
$body.=“
”;
函数sendmail($to、$subject、$message、$from){
$headers=“MIME版本:1.0”。\r\n”;
$headers.=“内容类型:text/html;字符集=iso-8859-1”。\r\n”;
$headers.='From:'.$From.\r\n;
$result=邮件($to、$subject、$message、$headers);
如果($result)返回1;
否则返回0;
}
$result=sendmail($to、$subject、$body、$email);
使用echo
将其返回脚本,而不是return
:
if ($result) echo 1;
else echo 0;
您也可以将其缩短,如:
echo $result ? 1 : 0;
使用
echo
将其返回脚本,而不是return
:
if ($result) echo 1;
else echo 0;
您也可以将其缩短,如:
echo $result ? 1 : 0;
在使用AJAX时,您不会返回
。您需要回显
值
if ($result) echo 1;
else echo 0;
在使用AJAX时,您不会返回。您需要回显
值
if ($result) echo 1;
else echo 0;
@Rocket:刚刚喝了咖啡:)这里有一个小附加问题,我可以添加一条要返回到ajax函数的消息吗,即返回1或0,如果是0,还可以返回一个字符串作为$string?@Nicola:是的,你也可以回显字符串。是的,但我是指参数,所以我想返回两个值(true,$string),你必须返回json。将dataType:'json'
添加到ajax函数中,并从php使用echo json_encode(数组('true'=>true,'string'=>'your string'))
。最后,从success函数中,您可以得到类似于html.true
和html.string
@Rocket:刚刚喝过咖啡:)这里有一个小附加问题,我可以添加一条要返回到ajax函数的消息吗,即返回1或0,如果是0,还可以将字符串作为$string返回?@Nicola:是的,您也可以回显字符串。是的,但我指的是参数,所以我想返回两个值(true,$string),您必须返回json。将dataType:'json'
添加到ajax函数中,并从php使用echo json_encode(数组('true'=>true,'string'=>'your string'))
。最后,从success函数中,您可以得到像html.true
和html.string