猿问

PHP 中的变量在 ajax 请求后丢失

我的目标 - 通过条纹或PayPal付款,一旦完成,向客户出示一份表格,上传文件以通过电子邮件发送给我。我的表单处于自举模态中。我正在尝试确保除非付款,否则无法提交表单。


客户当前付款,由 ajax 完成。这工作正常,并显示付款ID。但是,当我向同一 php 文档发送另一个 ajax 请求时,要完成电子邮件发送,使用 ,该变量已丢失其值。$send_form = $_POST["email_form"];$payment_made


<?php

$send_form = $_POST["email_form"]; //this is sent by ajax. Will be 'yes' if the form to send has been submitted.


switch ($action){

  case 'stripe':

    //do stripe transaction

    //set a variable to indicate payment is complete using transaction ID

    $payment_made = $transaction->id;

  break;

  case 'paypal':

    //do paypal transaction

    //set a variable to indicate payment is complete using transaction ID

    $payment_made = $transaction->id;

  break;

}


function sendmail() {

  //usual php mail function (works fine)

}


if ($send_form = 'yes' && $payment_made != '') {

  sendmail();

}

?>

我的问题:如何使用变量来验证付款是否已完成,以防止未经授权使用电子邮件表单?$payment_made


除非有要求,否则我不会上传ajax或html代码,因为它都可以正常工作,只要我在使用之前不检查变量。$payment_madephpmail()


慕码人8056858
浏览 78回答 1
1回答

繁花不似锦

我使用PHP会话来存储然后删除变量。<?phpsession_start();$send_form = $_POST["email_form"]; //this is sent by ajax. Will be 'yes' if the form to send has been submitted.switch ($action){&nbsp; case 'stripe':&nbsp; &nbsp; //do stripe transaction&nbsp; &nbsp; //set a variable to indicate payment is complete using transaction ID&nbsp; &nbsp; $_SESSION["payment_made"] = $transaction->id;&nbsp; break;&nbsp; case 'paypal':&nbsp; &nbsp; //do paypal transaction&nbsp; &nbsp; //set a variable to indicate payment is complete using transaction ID&nbsp; &nbsp; $_SESSION["payment_made"] = $transaction->id;&nbsp; break;}function sendmail() {&nbsp; //usual php mail function (works fine)&nbsp; session_unset();&nbsp; session_destroy();&nbsp; mail();}if ($send_form = 'yes' && $_SESSION["payment_made"] != '') {&nbsp; sendmail();&nbsp;}?>
随时随地看视频慕课网APP
我要回答