作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望表单根据选中的复选框来回显隐藏的表单字段,注意我需要在按下提交按钮之前回显隐藏的表单字段,因为它将发送到 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')) {
最佳答案
如果您没有提交表单,则没有 POST
或 GET
方法已执行,并且您在 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>
$(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.');
}
});
}
});
});
<?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);
}
?>
关于php - 如何根据php中的条件复选框回显隐藏的表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10436328/
我是一名优秀的程序员,十分优秀!