【问题标题】:Using POST from a function从函数中使用 POST
【发布时间】:2020-05-20 17:57:18
【问题描述】:

我有一个 PHP 脚本,我需要根据单选按钮选择转到三个不同 URL 之一,但我必须通过 POST 发送变量,因为一个 URL 是第三方页面(未显示),我不能改变它。

我有一个函数可以确定单选按钮的值,并指示脚本,但我没有找到控制从 GET 切换到 POST 的方法。

** 也许有比使用我不知道的函数更好的方法?

仅供参考-我尝试了许多按钮选项,包括按钮而不是输入;下面显示的只是我最近的状态。

<tr> 
  <th Align=Left vAlign=top rowSpan=6> <?=$THFont?> Payment Type</font></th>
  <td> <Input Name=PayType Type=Radio Value=CK ID=Check Checked>
       <Label for=Check> Check</label></td>
</tr>
<tr>
  <td> <Input Name=PayType Type=Radio Value=CS ID=Cash>
       <Label for=Cash> Cash</label></td>
</tr>
<tr>
  <td> <Input Name=PayType Type=Radio Value=CH ID=Charge>
       <Label for=Charge> Charge to Broker AR Account</label></td>
</tr>
<tr>
  <td> &nbsp; &nbsp; <input type=text name=PayNote id=PayNote placeholder='Payment Note (for 3 payment types above)' maxlength=30 size=35></td>
</tr>
<tr>
  <td> <Input Name=PayType Type=Radio Value=CC ID=Credit>
       <Label for=Credit> Credit Card  &nbsp;<i>(Visa, Master Card, American Express, Discover)</i></label></td>
</tr>
<tr>
  <td> <Input Name=PayType Type=Radio Value=NP ID=NoPay> 
       <Label for=NoPay> Reserve Without Payment</label></td>
</tr>


<input type="button" class="buttonProcess" value="Process" onclick="processPayment('<?=$EvntCode?>','<?=$SessionID?>','<?=$MbrID?>');">


  function processPayment(EvntCode,SessionID,MbrID) {
    var PayType = $("input:radio[name='PayType']:checked").val();
    var PayNote = document.getElementById('PayNote').value;
    if (PayType == "CK" || PayType == "CS" || PayType == "CH") {
      var url = "/apps/php/PayCashStaff_Process.php?EvntCode="+EvntCode+"&SessionID="+SessionID+"&MbrID="+MbrID+"&PayType="+PayType+"&PayNote="+PayNote;
      processPay = window.location.href=url;
    }
    if (PayType == "CC") {
      var url = "/apps/php/PayCCStaff_Home.php?EvntCode="+EvntCode+"&SessionID="+SessionID+"&MbrID="+MbrID;
      processCCPay = window.location.href=url;
    }
    if (PayType == "NP") {
      var url = "/apps/php/ConfirmReservation_Process.php?EvntCode="+EvntCode+"&SessionID="+SessionID+"&MbrID="+MbrID;
      confResWindow = window.location=(url);
    }
  }

【问题讨论】:

  • 您正在更改浏览器 URL 作为单击按钮的一部分。这总是一个 GET。如果你希望它是一个 POST,并且你希望页面作为结果转移到那个页面,你需要提交一个表单。
  • 除非有我不知道的方法,否则表单将不允许我评估选择以确定要定向到的 URL。使用表单给你“动作”,它是一个 URL,对吗?

标签: javascript php function post


【解决方案1】:

先构造表单,然后提交(不使用 AJAX)。表单将被提交,用户将被带到目的地:

function processPayment(EvntCode, SessionID, MbrID) {
  var PayType = $("input:radio[name='PayType']:checked").val();
  var PayNote = document.getElementById('PayNote').value;
  if (PayType == "CK" || PayType == "CS" || PayType == "CH") {
    var url = "/apps/php/PayCashStaff_Process.php?EvntCode=" + EvntCode + "&SessionID=" + SessionID + "&MbrID=" + MbrID + "&PayType=" + PayType + "&PayNote=" + PayNote;
    processPay = window.location.href = url;
  }
  if (PayType == "CC") {
    var url = "/apps/php/PayCCStaff_Home.php?EvntCode=" + EvntCode + "&SessionID=" + SessionID + "&MbrID=" + MbrID;
    processCCPay = window.location.href = url;
  }
  if (PayType == "NP2") {
    var url = "/apps/php/ConfirmReservation_Process.php?EvntCode=" + EvntCode + "&SessionID=" + SessionID + "&MbrID=" + MbrID;
    confResWindow = window.location = (url);
  }

  if (PayType == "NP") {
    var f = document.createElement("form");
    f.setAttribute('method', "post");
    f.setAttribute('action', "https://postman-echo.com/post");
    f.setAttribute('id', "postform");

    var i = document.createElement("input"); //input element, hidden
    i.setAttribute('type', "hidden");
    i.setAttribute('name', "EvntCode");
    i.setAttribute('value', EvntCode);
    f.appendChild(i);

    i = document.createElement("input"); //input element, hidden
    i.setAttribute('type', "hidden");
    i.setAttribute('name', "SessionID");
    i.setAttribute('value', SessionID);
    f.appendChild(i);

    i = document.createElement("input"); //input element, hidden
    i.setAttribute('type', "hidden");
    i.setAttribute('name', "MbrID");
    i.setAttribute('value', MbrID);
    f.appendChild(i);

    document.body.appendChild(f);

    f.submit();
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <th Align=Left vAlign=top rowSpan=6>
      <?=$THFont?> Payment Type</font>
    </th>
    <td>
      <Input Name=PayType Type=Radio Value=CK ID=Check Checked>
      <Label for=Check> Check</label>
    </td>
  </tr>
  <tr>
    <td>
      <Input Name=PayType Type=Radio Value=CS ID=Cash>
      <Label for=Cash> Cash</label>
    </td>
  </tr>
  <tr>
    <td>
      <Input Name=PayType Type=Radio Value=CH ID=Charge>
      <Label for=Charge> Charge to Broker AR Account</label>
    </td>
  </tr>
  <tr>
    <td> &nbsp; &nbsp; <input type=text name=PayNote id=PayNote placeholder='Payment Note (for 3 payment types above)' maxlength=30 size=35></td>
  </tr>
  <tr>
    <td>
      <Input Name=PayType Type=Radio Value=CC ID=Credit>
      <Label for=Credit> Credit Card  &nbsp;<i>(Visa, Master Card, American Express, Discover)</i></label>
    </td>
  </tr>
  <tr>
    <td>
      <Input Name=PayType Type=Radio Value=NP ID=NoPay>
      <Label for=NoPay> Reserve Without Payment</label>
    </td>
  </tr>
</table>
<input type="button" class="buttonProcess" value="Process" onclick="processPayment('fooEvent','fooSess','fooMbr');">

注意:可以制作一个不错的表单构造函数,但你明白了。

【讨论】:

  • 我是 javascript 的新手,您添加的以 if (PayType == "TypeToPost"){ 开头的代码和 setAttribute 等对我来说是新的。无论如何,我复制了你提供的所有代码,当我运行它时,我被要求授权登录到我们的系统,我觉得这很奇怪,但尝试登录并失败它失败了。
  • @BrianMiller 只需将TypeToPost 替换为您想要的任何内容(CK、CC 等)。我不确定您要发布到哪个条件/链接而不是 GET。
  • 我能够获取要处理的代码,但我的变量在接收脚本中是空白的,所以我假设我缺少一块。我在你之后添加了这段代码: var url = "/apps/php/ConfirmReservation_Process.php"; confResWindow = window.location=(url);
  • 另一个注意事项,我的表单如下所示:
    我知道这些字段在函数中具有价值,因为我发出了警报测试值。
  • @BrianMiller 已修复。我忘了将表格附加到正文中。您必须将内联 PHP 添加回 HTML。 NP 条件用于示例目的。
【解决方案2】:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
  function processPayment2(EvntCode,SessionID,MbrID) {
    var PayType = $("input:radio[name='PayType']:checked").val();
    var PayNote = document.getElementById('PayNote').value;
    if (PayType == "NP") {
      var f = document.createElement("form");
      f.setAttribute('method',"POST");
      f.setAttribute('action',"https://xxxxxxx.xxxx.com/apps/php/member/Event/ConfirmReservation_Process.php");
      f.setAttribute('id',"postform");

      var i = document.createElement("input"); //input element, hidden
      i.setAttribute('type',"hidden");
      i.setAttribute('name',"EvntCode");
      i.setAttribute('value', EvntCode);
      f.appendChild(i);

      i = document.createElement("input"); //input element, hidden
      i.setAttribute('type',"hidden");
      i.setAttribute('name',"SessionID");
      i.setAttribute('value', SessionID);
      f.appendChild(i);

      i = document.createElement("input"); //input element, hidden
      i.setAttribute('type',"hidden");
      i.setAttribute('name',"MbrID");
      i.setAttribute('value', MbrID);
      f.appendChild(i);

      document.body.appendChild(f);

      f.submit();
    }
  }
</script>


<table cellSpacing=5>
  <tr>
    <th Align=Left> <?=$THFont?> Event Code: </font></th>
    <td> <?=$TDFont?> <?=$EvntCode?> </td>
  </tr>
  <tr>
    <th Align=Left> <?=$THFont?> Session:</font></th>
    <td> <?=$TDFont?> <?=$SessionID?> </td>
  </tr>
  <tr>
    <th Align=Left> <?=$THFont?> Title:</font></th>
    <td> <?=$TDFont?> <?=trim($event_row["TITLE"])?> </td>
  </tr>
  <tr> 
    <th Align=Left> <?=$THFont?> Name:</font></th>
    <td> <?=$TDFont>> <?=trim($MbrMst02_MNAME)?></td>
  </tr>
  <tr>
    <th Align=Left> <?=$THFont?> Email:</font></th>
    <td> <?=$TDFont?> <?=trim($MbrMst02_MBWADR)?> </td>
  </tr>
  <tr> 
    <th Align=Left> <?=$THFont?> Amount:</font></th>
    <td> <?=$TDFont?> <?=money_format('%(#10n', $zAmount)?> </td>
  </tr>
  <tr>
    <th Align=Left> <?=$THFont?> Transaction Number:</font></th>
    <td> <?=$TDFont?> <?=$zTrnID?> </td>
  </tr>
  <tr>
    <td colSpan=2><hr>
  </tr>
  <tr>
    <th Align=Left vAlign=top rowSpan=6> <?=$THFont?> Payment Type</font></th> 
    <td> <Input Name=PayType Type=Radio Value=CK ID=Check Checked>
         <Label for=Check> Check</label></td>
  </tr>
  <tr>
    <td> <Input Name=PayType Type=Radio Value=CS ID=Cash>
         <Label for=Cash> Cash</label></td>
  </tr>
  <tr>
    <td> <Input Name=PayType Type=Radio Value=CH ID=Charge>
         <Label for=Charge> Charge to Broker AR Account</label></td>
  </tr>
  <tr>
    <td> &nbsp; &nbsp; <input type=text name=PayNote id=PayNote placeholder='Payment Note (for 3 payment types above)' maxlength=30 size=35></td>
  </tr>
  <tr>
    <td> <Input Name=PayType Type=Radio Value=CC ID=Credit> 
         <Label for=Credit> Credit Card  &nbsp;<i>(Visa, Master Card, American Express, Discover)</i></label></td>
  </tr>
  <tr>
    <td> <Input Name=PayType Type=Radio Value=NP ID=NoPay>
         <Label for=NoPay> Reserve Without Payment</label></td>
  </tr>
</table>

<input type="button" class="buttonProcess" value="Process" onclick="processPayment2('<?=$EvntCode?>','<?=$SessionID?>','<?=$MbrID?>');">

View screen snippet here

【讨论】:

  • 您不必这样做。你可以编辑我的答案。在这个网站上这样做更合适。
猜你喜欢
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多