【问题标题】:How to store two values in php variables, that have been passed via get JSON如何在通过get JSON传递的php变量中存储两个值
【发布时间】:2014-02-12 12:26:15
【问题描述】:

我想将这两个值存储在 php 中的 calcsums.php 中,然后通过 GET JSON 从 Jquery 获取它们。我怎样才能做到这一点?我不想将它们存储在 Jquery 中,我想将它们存储在 PHP(在 calcssums.php 中)

我的表单

<form action="" method="post" name="formsubmit" id="formsubmit"   >
<h1> Process </h1>
<p> operation type always robot </p>
<br> <br>
Number of welds: <input type="number" name="numberofwelds" id="numberofwelds"  >
<br> <br>
Number of construction welds: <input type="number" name="numberofconwelds" id="numberofconwelds"  >
<br> <br>
<h2> Outputs </h2>
<br> <br>
Robot in/out: <input type="text" name="robotinout" disabled>
<br> <br>
Weld time: <input type="text" name="weldtime" disabled>
<br> <br>
Controlling cycle: <input type="text" name="concycle" disabled>
<br> <br>
Total time(secs): <input type="text" name="totaltimesecs" disabled>
<br> <br> <br> <br>
<input type="submit"  value="Calculate" id="submit" name="submit">
<div id="result"> </div>
</form>

jQuery。从两个字段中获取值。不是整个表格

 var formData = $('#numberofwelds, #numberofconwelds').serialize();
    $.get('calcsums.php', formData, sumresults);

【问题讨论】:

  • 你提到的 PHP 变量在哪里?
  • @jon 对不起。不要以为我的问题很清楚。我想将 PHP 中的两个字段值存储在 calcssums.php 中。这样我就可以对它们进行一些计算。
  • 您可以使用$_GET['numberofwelds'] 等访问这些值。
  • 感谢支持

标签: php jquery serialization


【解决方案1】:

试试这个:

var numberofwelds = $('#numberofwelds').val(),
    numberofconwelds= $('#numberofconwelds').val();

$.get( 
     "calcsums.php", 
     { numberofwelds : numberofwelds , numberofconwelds: numberofconwelds }, 
     sumresults  
);

然后就可以在 PHP 中检索该值了:

$numberofwelds = $_GET['numberofwelds'];
$numberofconwelds = $_GET['numberofconwelds'];

【讨论】:

  • “这个答案没用”是 投反对票的原因。如果你认为它真的有用,你能解释一下为什么吗?这将以什么方式解决问题?提示:你会发现很难解释,因为问题甚至没有被正确定义。
  • 好的,现在它正朝着正确的方向发展。 JS部分仍然无关紧要。
  • @Felix 对不起。我的问题还不够清楚。我想将字段值存储在 php 中的 calcsums.php 中。不要将它们存储在javascript中
【解决方案2】:

如果您想将所有表单数据作为 JSON,请使用以下命令:

var formData = $( "#formsubmit" ).serialize();

如果您只想获取某些字段,请使用该字段值创建一个 JSON,例如:

var formData  = {numberofwelds:$('#numberofwelds').val(), numberofconwelds:$('#numberofconwelds').val()}

【讨论】:

  • @Sherin Jose 在通过 GET JSON 从 Jquery 获取这两个值之后,我想将这两个值存储在 php 中的 calcsums.php 中。很抱歉造成混乱
  • 您可以通过$_GET['numberofwelds']$_GET['numberofconwelds']访问php文件(calcsums.php)中的值
  • 感谢您的支持
猜你喜欢
  • 2019-06-05
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
相关资源
最近更新 更多