我想在php中显示所有选中的复选框。
<script>
function RequestObject ()
{
var ReturnValue = null;
try {
ReturnValue = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (Error) {
try {
ReturnValue = new ActiveXObject("MSXML2.HTTP");
}
catch (Error) {
try {
ReturnValue = new XMLHttpRequest();
}
catch (Error) {
// failed
return null;
}
}
}
return ReturnValue;
}
var requestObj = null;
function coajax(sender)
{
requestObj = RequestObject();
if (requestObj)
{
sender.enabled = false; // so a user cannot change till request finished
requestObj.open('get', '<?php echo get_template_directory_uri(); ?>/lunch_dinner.php?lid=' + sender.value, true);
requestObj.onreadystatechange = coajax_finish;
requestObj.send(null);
}
}
function coajax_finish()
{
if (requestObj.readyState == 4)
{
document.getElementById('ldb_target').innerHTML = requestObj.responseText;
document.getElementById('ldb').enabled = true;
}
}
function getDays(select){
var selectedString = select.options[select.selectedIndex].value;
if(selectedString == "Bespoke Days"){
document.getElementById("days_target").style.display = "block";
}
else {
document.getElementById("days_target").style.display = "none";
}
}
function validateForm()
{
var x=document.forms["form1"]["contract"].value;
if (x==null || x=="" || x=="Select Contract")
{
alert("Oh, you forgot to select contract type! :)");
return false;
}
var x=document.forms["form1"]["days"].selectedIndex;
if (x<1)
{
alert("Please select days");
return false;
}
else if(x==4) { // fifth entry
var checked = false,
chk = document.forms["form1"]["day"];
for (var i = 0; i < chk.length; i++)
{
if (chk[i].checked) { checked=true; break }
}
if (!checked) {
alert("At least one day should be checked.");
return false;
}
}
var x=document.forms["form1"]["ldb"].value;
if (x==null || x=="" || x=="Select")
{
alert("Oh, you forgot to select lunch-dinner! :)");
return false;
}
var x=document.forms["form1"]["ldb"].value;
if(x=="Lunch" || x=="Both")
{
var y=document.forms["form1"]["lunch"].value;
if(y=="Lunch")
{
alert("Oh, you forgot to select lunch time! :)");
return false;
}
}
var x=document.forms["form1"]["ldb"].value;
if(x=="Dinner" || x=="Both")
{
var y=document.forms["form1"]["dinner"].value;
if(y=="Dinner")
{
alert("Oh, you forgot to select dinner time! :)");
return false;
}
}
var x=document.forms["form1"]["phone"].value;
if (x==null || x=="")
{
alert("Oh, you forgot to enter your Phone Number! :)");
return false;
}
var x = document.form1.phone.value;
if(isNaN(x)||x.indexOf(" ")!=-1)
{
alert("Please enter Numaric value in contact number.")
return false;
}
var x=document.forms["form1"]["meal"].value;
if (x==null || x=="" || x=="Select Meal")
{
alert("Oh, you forgot to select meal preference! :)");
return false;
}
var x=document.forms["form1"]["fnm"].value;
if (x==null || x=="")
{
alert("Oh, you forgot to enter your Full Name! :)");
return false;
}
var x=document.forms["form1"]["pcod"].value;
if (x==null || x=="")
{
alert("Oh, you forgot to enter your Postal Code! :)");
return false;
}
var x=document.forms["form1"]["email"].value;
if (x==null || x=="")
{
alert("Oh, you forgot to enter your Email Address! :)");
return false;
}
var x=document.forms["form1"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Excuse me! Valid Email Address Please!");
return false;
}
var x=document.forms["form1"]["add"].value;
if (x==null || x=="")
{
alert("Oh, you forgot to enter your Address! :)");
return false;
}
var x=document.forms["form1"]["rate"].value;
if (x==null || x=="")
{
alert("Oh, you forgot to enter Rate! :)");
return false;
}
var x=document.forms["form1"]["rate"].value;
if(isNaN(x)||x.indexOf(" ")!=-1)
{
alert("Please enter Numaric value in Rate.")
return false;
}
var x=document.forms["form1"]["amount"].value;
if (x==null || x=="")
{
alert("Oh, you forgot to enter Amount! :)");
return false;
}
var x=document.forms["form1"]["amount"].value;
if(isNaN(x)||x.indexOf(" ")!=-1)
{
alert("Please enter Numaric value in Total Amount.")
return false;
}
}
function calculateTotal(rate)
{
var rate=(document.forms["form1"]["rate"].value) * 1.03;
document.forms["form1"]["amount"].value = rate;
}
</script>
HTML:
<form method="POST" action="" name="form1" onsubmit="return validateForm();">
<table class="order-paypal" width="100%">
<tr>
<td><span>Choose contract</span></td>
<td>
<select name="contract">
<option value="Select Contract" selected>Select Contract</option>
<option value="7 meal veg contract">7 meal veg contract</option>
<option value="7 meal nonveg contract">7 meal nonveg contract</option>
<option value="7 meal nonveg contract">7 meal nonveg contract</option>
<option value="8 meal veg contract">8 meal veg contract</option>
<option value="8 meal nonveg contract">8 meal nonveg contract</option>
<option value="8 meal mix contract">8 meal mix contract</option>
<option value="9 meal veg contract">9 meal veg contract</option>
<option value="9 meal nonveg contract ">9 meal nonveg contract </option>
<option value="9 meal mix contract ">9 meal mix contract </option>
<option value="30 meal veg contract">30 meal veg contract</option>
<option value="30 meal nonveg contract">30 meal nonveg contract</option>
<option value="30 meal mix contract">30 meal mix contract</option>
<option value="Book-a-cook service">Book-a-cook service</option>
<option value="Extra roti/rice/dal/vegetable">Extra roti/rice/dal/vegetable</option>
</select>
</td>
</tr>
<tr>
<td><span>Please enter days required</span></td>
<td>
<select name="days" id="days" onchange="getDays(this)">
<option value="Select Days" selected>Select Days</option>
<option value="Mon-Fri">Mon-Fri</option>
<option value="Mon-Sat">Mon-Sat</option>
<option value="Mon-Sun">Mon-Sun</option>
<option value="Bespoke Days">Bespoke Days</option>
</select>
<ul id="days_target" style="display:none;">
<span>Select Days</span>
<li><input type="checkbox" name="day[]" value="mon"/>Monday</li>
<li><input type="checkbox" name="day[]" value="tue"/>Tueday</li>
<li><input type="checkbox" name="day[]" value="wed"/>Wednesday</li>
<li><input type="checkbox" name="day[]" value="thr"/>Thursday</li>
<li><input type="checkbox" name="day[]" value="fri"/>Friday</li>
<li><input type="checkbox" name="day[]" value="sat"/>Saturday</li>
<li><input type="checkbox" name="day[]" value="sun"/>Sunday</li>
</ul>
</td>
</tr>
<tr>
<td><span>Please mention Lunch or Dinner or both</span></td>
<td>
<select name="ldb" id="ldb" onchange="coajax(this);" >
<option value="Select" selected>Select</option>
<option value="Lunch">Lunch</option>
<option value="Dinner">Dinner</option>
<option value="Both">Both</option>
</select>
<span id="ldb_target"></span><br/>
</td>
</tr>
<tr>
<td><span>Please mention your contact number</span></td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td><span>Please mention your meal preference</span></td>
<td>
<select name="meal" >
<option value="Select Meal" selected>Select Meal</option>
<option value="Punjabi">Punjabi</option>
<option value="Gujarati">Gujarati</option>
</select>
</td>
</tr>
<tr>
<td style="vertical-align:top;"><span>Please mention any dietary requirements</span></td>
<td><input type="text" name="req" ></td>
</tr>
<tr>
<td><span>Full name</span></td>
<td><input type="text" name="fnm" ></td>
</tr>
<tr>
<td><span>Address</span></td>
<td><textarea name="add" ></textarea></td>
</tr>
<tr>
<td><span>Postal code</span></td>
<td><input type="text" name="pcod" ></td>
</tr>
<tr>
<td><span>Email</span></td>
<td><input type="text" name="email" ></td>
</tr>
<tr>
<td><span>Enter amount to pay</span></td>
<td>
<input type="text" name="rate" >
<br />
(<a target="_blank" href="http://www.infozzle.com/yatin/tifffin_planet/prices-and-offers-2/">Check the rate of your item and your zone</a>)
<br/>
Card charge: 3% of the total amount
</td>
</tr>
<tr>
<td><span>Total Amount</span></td>
<td><input type="text" name="amount" value="Click here to see total amount" onclick="calculateTotal(this);"></td>
</tr>
<tr>
<td> </td>
<td>
<table class="submit">
<tr>
<td><input type="submit" value="Submit" name="submitbtn" class="submit-payment"/></td>
<td><input type="reset" value="Reset" class="submit-reset"/></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
我想通过以下代码在操作页面上显示所有选中的“日期”复选框:
<?php
foreach($day as $k)
{
echo $k.",";
}
?>
html表单和操作页面位于wordpress的模板页面中。 但是它不起作用。我写了这段代码。
答案 0 :(得分:1)
试试这个,
<form name="frmli" action="" method="post"><ul>
<li><input type="checkbox" name="day[]" value="mon"/>Monday</li>
<li><input type="checkbox" name="day[]" value="tue"/>Tueday</li>
<li><input type="checkbox" name="day[]" value="wed"/>Wednesday</li>
<li><input type="checkbox" name="day[]" value="thr"/>Thursday</li>
<li><input type="checkbox" name="day[]" value="fri"/>Friday</li>
<li><input type="checkbox" name="day[]" value="sat"/>Saturday</li>
<li><input type="checkbox" name="day[]" value="sun"/>Sunday</li>
<li><input type="submit" name="submit" value="submit"/></li>
</ul>
</form>
<?php
if(isset($_POST['submit'])){
foreach($_POST['day'] as $day)
{
echo $day." ";
}
}
?>
答案 1 :(得分:1)
这可能会对您有所帮助:
<form name="search_field" id="search_field" action="formpage.php" method="post" >
<ul>
<li><input type="checkbox" name="day[]" value="mon"/>Monday</li>
<li><input type="checkbox" name="day[]" value="tue"/>Tueday</li>
<li><input type="checkbox" name="day[]" value="wed"/>Wednesday</li>
<li><input type="checkbox" name="day[]" value="thr"/>Thursday</li>
<li><input type="checkbox" name="day[]" value="fri"/>Friday</li>
<li><input type="checkbox" name="day[]" value="sat"/>Saturday</li>
<li><input type="checkbox" name="day[]" value="sun"/>Sunday</li>
</ul>
<input type="submit" id="submit" name="submit" />
</form>
您可以从中获得工作结果(为了更好地理解):
<form name="search_field" id="search_field" action="formpage.php" method="post" >
<?php if(!$_POST == ''){ ?>
<ul>
<li><input type="checkbox" <?php if(in_array("mon", $_POST['day'])) {?> checked="checked"<?php } ?> name="day[]" value="mon"/>Monday</li>
<li><input type="checkbox" <?php if(in_array("tue", $_POST['day'])) {?> checked="checked"<?php } ?> name="day[]" value="tue"/>Tueday</li>
<li><input type="checkbox" <?php if(in_array("wed", $_POST['day'])) {?> checked="checked"<?php } ?> name="day[]" value="wed"/>Wednesday</li>
<li><input type="checkbox" <?php if(in_array("thr", $_POST['day'])) {?> checked="checked"<?php } ?> name="day[]" value="thr"/>Thursday</li>
<li><input type="checkbox" <?php if(in_array("fri", $_POST['day'])) {?> checked="checked"<?php } ?> name="day[]" value="fri"/>Friday</li>
<li><input type="checkbox" <?php if(in_array("sat", $_POST['day'])) {?> checked="checked"<?php } ?> name="day[]" value="sat"/>Saturday</li>
<li><input type="checkbox" <?php if(in_array("sun", $_POST['day'])) {?> checked="checked"<?php } ?> name="day[]" value="sun"/>Sunday</li>
</ul>
<input type="submit" id="submit" name="submit" />
</form>
<?php
print_r($_POST);
?>
<?php } else { ?>
<form name="search_field" id="search_field" action="formpage.php" method="post" >
<ul>
<li><input type="checkbox" name="day[]" value="mon"/>Monday</li>
<li><input type="checkbox" name="day[]" value="tue"/>Tueday</li>
<li><input type="checkbox" name="day[]" value="wed"/>Wednesday</li>
<li><input type="checkbox" name="day[]" value="thr"/>Thursday</li>
<li><input type="checkbox" name="day[]" value="fri"/>Friday</li>
<li><input type="checkbox" name="day[]" value="sat"/>Saturday</li>
<li><input type="checkbox" name="day[]" value="sun"/>Sunday</li>
</ul>
<input type="submit" id="submit" name="submit" />
</form>
<?php } ?>
只需创建formpage.php文件,粘贴上面的代码并检查结果。
答案 2 :(得分:0)
确保您设置的表单标记如下: method="post"
。还要先检查是否已设置POST['day']
。我在这种情况下使用了count()。我添加了if else语句,这样如果您的$_POST['day']
为空,那么您至少会回应它是空的。
<form name="frmli" action="" method="POST">
<ul>
<li><input type="checkbox" name="day[]" value="mon"/>Monday</li>
<li><input type="checkbox" name="day[]" value="tue"/>Tueday</li>
<li><input type="checkbox" name="day[]" value="wed"/>Wednesday</li>
<li><input type="checkbox" name="day[]" value="thr"/>Thursday</li>
<li><input type="checkbox" name="day[]" value="fri"/>Friday</li>
<li><input type="checkbox" name="day[]" value="sat"/>Saturday</li>
<li><input type="checkbox" name="day[]" value="sun"/>Sunday</li>
<input type="submit" value="submit" name="submit" />
</ul>
</form>
<?php
if(empty($_POST['day'])){
echo "none of the boxes were checked";
}else{
$N = count($_POST['day']);
echo("You selected $N door(s): ");
for($i=0; $i < $N; $i++){
echo($_POST['day'][$i] . " ");
}
}
?>