gpt4 book ai didi

php - 如何根据php中的条件复选框回显隐藏的表单字段?

转载 作者:行者123 更新时间:2023-12-04 18:22:09 26 4
gpt4 key购买 nike

我希望表单根据选中的复选框来回显隐藏的表单字段,注意我需要在按下提交按钮之前回显隐藏的表单字段,因为它将发送到 Paypal 。这是我到目前为止所拥有的 -

<?php       
echo ('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">');
echo ('<input type="hidden" name="cmd" value="_cart">');
echo ('<input type="hidden" name="upload" value="1">');
echo ('<input type="hidden" name="business" value="youremail@mail.com">');
echo ('<input type="hidden" name="currency_code" value="US">');
echo ('<input type="checkbox" name="camp1" value="a">');
echo ('<input type="checkbox" name="camp2" value="b">');

if (isset($_POST['camp1'])) {

echo ("<input type='hidden' name='item_name_1' value='beach ball'>");
("<input type='hidden' name='amount_1' value=50'>");
}
if (isset($_POST['camp2'])) {

echo ("<input type='hidden' name='item_name_2' value='towel'>");
("<input type='hidden' name='amount_2' value='20'>");
}
echo ("<input type='submit' value='PayPal'>");
("</form>");
?>

我也试过更换
if (isset($_POST['camp1'])) {

有类似的东西
if(IsChecked('camp1','a')) {

没有运气。

任何帮助将非常感激!

最佳答案

如果您没有提交表单,则没有 POSTGET方法已执行,并且您在 if 语句中调用来自 POST $_POST['camp1'] 的变量你永远不会得到那个值(value)。解决此问题的替代方法是使用 JS 或 jQuery,例如:

您修改后的 PHP:

<html>
<head>
<title>Paying process</title>
</head>
<script scr="jquery-1.7.2.js" type="text/javascript" />
<script scr="functions.js" type="text/javascript" />
<body>
<?php
echo ('<form id="payForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
echo ('<input type="hidden" name="cmd" value="_cart">');
echo ('<input type="hidden" name="upload" value="1">');
echo ('<input type="hidden" name="business" value="youremail@mail.com">');
echo ('<input type="hidden" name="currency_code" value="US">');
echo ('<input type="checkbox" name="camp1" value="a">');
echo ('<input type="checkbox" name="camp2" value="b">');
echo ('<div id="productInfo" style="display:none;"></div>');
echo ("<input type='submit' value='PayPal'>");
echo ("</form>");
?>
</body>
</html>

functions.js 文件:
$(document).ready(function(){

$("#payForm").submit(function(e){
if($('#productInfo').html() == ''){
//if there is no product in the hidden div, prevent the submit and show msg.
alert('Please select at least one product');
e.preventDefault();
}
});

// event when user clicks a checkbox
$("input[type='checkbox']").on('click', function(){
var checked = $(this).attr('checked');
if(checked){
var value = $(this).val();
$.post('product.php', { value:value }, function(data){
// data = 0 - means that there was an error or no product
if(data != 0){
// At this point you will have your "a" or "b" products in hidden
// Also submit button will appear
// This means that everything was Ok and the user selected a product
$('#productInfo').append(data);
} else {
// data = 0
alert('Please try again.');
}
});
}
});

});

product.php 文件 (在 jQuery 帖子中使用):
  <?php
if ($_POST && isset($_POST['value'])) {

// db connection
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
// error happened
print(0);
}
mysql_select_db('mydb');

// sanitize the value
$value = mysql_real_escape_string($_POST['value']);

// start the query - I am assuming one :-)
$sql = "SELECT * FROM products WHERE Id = '$value'";
$result = mysql_query($sql, $link);

// check if the product exists
if(mysql_num_rows($result) > 0){
while($row = mysql_fetch_array($result)){
if($value == 'a'){
echo ("<input type='hidden' name='item_name_1' value='".$row['product_name']."' />");
echo ("<input type='hidden' name='amount_1' value='".$row['product_price']."'>");
} else {
echo ("<input type='hidden' name='item_name_2' value='".$row['product_name']."' />");
echo ("<input type='hidden' name='amount_2' value='".$row['product_price']."'>");
}
}
} else {
// no product found
print(0);
}
?>

现在,只有当用户至少选择一种产品时,您才能提交隐藏值、项目 1 或 2 或两者。

希望这可以帮助 :-)

关于php - 如何根据php中的条件复选框回显隐藏的表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10436328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com