如何在php变量中存储两个值,这些值已经通过get JSON传递

时间:2014-02-12 12:26:16

标签: php jquery serialization

我希望在从Jquery通过GET JSON获取它们之后,在calcsums.php中将这两个值存储在php中。我怎样才能做到这一点?我不想将它们存储在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);

2 个答案:

答案 0 :(得分:1)

试试这个:

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

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

然后您可以在PHP中检索值:

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

答案 1 :(得分:1)

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

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

如果您只想使用某些字段,请使用以下字段值生成JSON:

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