获取表单输入值并传递给php变量

时间:2014-03-04 09:11:55

标签: php forms post

我正在配置在php中实现的tpv。

这是tpv commerce给我的文件:

form.php

<?PHP


// If form is submitted with all required data then show the form
// else show error page
empty($Formulario) ?              
    ShowForm($Ds_Merchant_Amount,$Ds_Merchant_Currency,$prod) :
    ShowError();
exit;
?>

<?PHP

function ShowError () {
  echo "<table width=100% height=50%><tr><td><p><h2><center>Compruebe que todos los datos del formulario son correctos!!</center></h2></p></td></tr></table>\n";
} # End of function ShowError

function ShowForm ($amount,$currency,$producto) {
// Posted data
global $_POST;

// Valores constantes del comercio
$url_tpvv='xxxxxxxxxxxx';
$clave='xxxxxxxxxx';
$name='Panel piedra';
$code='xxxxxxxxxxxxxxx';
$terminal='1';
$order=date('ymdHis');
$amount = '50'; //importe
$currency='978';
$transactionType='0';
$urlMerchant=''; //ruta a fichero que notifica por email
$producto = 'Zapatos';
//$producto = '<script>'$('#requiredinput1').val()'</script>'; //nºfactura y producto


// Now, print the HTML script
echo "
<script language=JavaScript>
function calc() { 

$('#Ds_Merchant_Amount').val( $('#requiredinput2').val() );
$('#Ds_Merchant_Producto').val( $('#requiredinput1').val() );

if($('#requiredinput1').val()==''){
  alert('Es necesario introducir nºfactura y concepto');
  return;
}
else if($('#requiredinput2').val()==''){
  alert('Es necesario introducir el importe de la factura');
  return;
}
else if($('#requiredinput3').val()==''){
  alert('Es necesario introducir el email');
  return;
}


vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
document.forms[0].submit();}
</script>
<body bgcolor=white>
<form name=compra action=$url_tpvv method=post target=tpv>
<pre>
<table>
<tr><td>";

echo "</td>
</tr><tr><td>
<input type='text' name='requiredinput1' id='requiredinput1' placeholder='Introduzca nºfactura y concepto' style='width: 250px;height: 30px;'/><br> 
<input type='text' name='requiredinput2' id='requiredinput2' placeholder='Introduzca el importe de la factura' style='width: 250px;height: 30px;margin-top: 1em;'/> <br>
<input type='text' name='requiredinput3' id='requiredinput3' placeholder='Introduzca email' style='width: 250px;height: 30px;margin-top: 1em;margin-bottom: 1em;'/> <br>

<input type='hidden' name='Ds_Merchant_Amount' value='$amount' />
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Currency value='$currency'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Producto value='$producto'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Order  value='$order'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantCode value='$code'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Terminal value='$terminal'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_TransactionType value='$transactionType'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantURL value='$urlMerchant'>
</td></tr><tr><td>";

// Compute hash to sign form data
// $signature=sha1_hex($amount,$order,$code,$currency,$clave);
$message = $amount.$order.$code.$currency.$transactionType.$urlMerchant.$clave;
$signature = strtoupper(sha1($message));

echo "<input type=hidden name=Ds_Merchant_MerchantSignature value='$signature'>
</td></tr>
</table>
<center><a href='javascript:calc()' class='realizarpago'>Realizar pago</a></center>
</pre>
</form>                     
";
} # End of function ShowForm
?>

例如,观察金额。它是一个变量,具有常量值,但我需要为其分配用户引入的值。 你能帮帮我吗?

谢谢,丹尼尔

2 个答案:

答案 0 :(得分:1)

您可以通过$_POST superglobal访问提交的表单值。

例如,如果您的表单字段名为amount,则可以使用$_POST['amount']访问该值。这是您可以为脚本中的$amount变量指定的值。

您不需要global $_POST;行。

参考:http://php.net/manual/en/reserved.variables.post.php

编辑:处理表单输入时,请不要忘记sanitize

答案 1 :(得分:0)

我终于解决了它。 我不得不将输入可见字段(用户必须填充)移动到另一个第一个表单。

<强> firstform.php

<form action="tpv.php" method="POST">
<input type='text' name='requiredinput1' id='requiredinput1' placeholder='Introduzca nºfactura y concepto' style='width: 250px;height: 30px;'/><br> 
<input type='text' name='requiredinput2' id='requiredinput2' placeholder='Introduzca el importe de la factura' style='width: 250px;height: 30px;margin-top: 1em;'/> <br>
<input type='text' name='requiredinput3' id='requiredinput3' placeholder='Introduzca email' style='width: 250px;height: 30px;margin-top: 1em;margin-bottom: 1em;'/> <br>
<input type="submit" value="Realizar pago" />
</form>

这是第一个,我填充三个数据(描述,数量,电子邮件),并将它们发送到tpv.php。所以,在tpv.php中,我使用$ _POST

来获取它们

<强> tpv.php

<?PHP


// If form is submitted with all required data then show the form
// else show error page
empty($Formulario) ?              
    ShowForm($Ds_Merchant_Amount,$Ds_Merchant_Currency,$prod) :
    ShowError();
exit;
?>

<?PHP

function ShowError () {
  echo "<table width=100% height=50%><tr><td><p><h2><center>Compruebe que todos los datos del formulario son correctos!!</center></h2></p></td></tr></table>\n";
} # End of function ShowError

function ShowForm ($amount,$currency,$producto) {
// Posted data
global $_POST;

// Valores constantes del comercio
$url_tpvv='xxxx';
$clave='xxxx';
$name='Panel piedra';
$code='xxxx';
$terminal='1';
$order=date('ymdHis');
$amount = $_POST['requiredinput2']; //importe
$currency='978';
$transactionType='0';
$urlMerchant=''; //ruta a fichero que notifica por email
$producto = $_POST['requiredinput1'];


// Now, print the HTML script
echo "

<body bgcolor=white>
<form name=compra action=$url_tpvv method=post target=tpv>
<pre>
<table>
<tr><td>";

echo "</td>
</tr><tr><td>


<input type='hidden' name='Ds_Merchant_Amount' value='$amount' />
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Currency value='$currency'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Producto value='$producto'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Order  value='$order'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantCode value='$code'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Terminal value='$terminal'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_TransactionType value='$transactionType'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantURL value='$urlMerchant'>
</td></tr><tr><td>";

// Compute hash to sign form data
// $signature=sha1_hex($amount,$order,$code,$currency,$clave);
$message = $amount.$order.$code.$currency.$transactionType.$urlMerchant.$clave;
$signature = strtoupper(sha1($message));

echo "<input type=hidden name=Ds_Merchant_MerchantSignature value='$signature'>
</td></tr>
</table>

</pre>
</form>



<script language=JavaScript>  
    vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
    document.forms[0].submit();

</script>

";
} # End of function ShowForm
?>

我希望这个答案可以帮助其他用户。 问候。