I want to display all checked checkboxes in 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>
I want to display all checked checkboxes of “day” on action page by this code:
<?php
foreach($day as $k)
{
echo $k.",";
}
?>
html Form and action page are in template page of wordpress.
but it doesn’t work.how to do?i have written this code.
This might help you:
You can get the working result from this (For your better understanding):
Just create the formpage.php file, paste the above code and check the result.
try this,
Make sure you have the form tag set like this:
method="post"
. also first check to see that thePOST['day']
has been set or not. I used count() in this situation. I added the if else statement so that if your$_POST['day']
is empty, you’ll at least be echoing that it’s empty.