I have a jQuery mobile form with the data posting to a review.php form. When there is no data entered in an array of check boxs (no data needed from user) the review returns
Warning: join() [function.join]: Invalid arguments passed in /hermes/waloraweb076/b2830/moo.revolveis/hg/order/review.php on line 35
Here is a sample of the form:
<li data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose Grind:</legend>
<input type="checkbox" name="grind51[vgrind]" id="vgrind" class="custom" value="V-Grind">
<label for="vgrind">V-Grind</label>
<input type="checkbox" name="grind51[tourgrind]" id="tourgrind" class="custom" value="Tour-Grind">
<label for="tourgrind">Tour Grind</label>
<input type="checkbox" name="grind51[healgrind]" id="healgrind" class="custom" value="Heal-Grind">
<label for="healgrind">Heal Grind</label>
<input type="checkbox" name="grind51[nogrind]" id="nogrind" class="custom" value="No Grind">
<label for="nogrind">No Grind</label>
</fieldset>
</li>
Here is a sample of the review.php:
<?php
$grind51 = join(", ", $_REQUEST["grind51"]);
echo (!empty($_REQUEST['grind51'])) ? "<div class='reviewItem'><span class='reviewTitle'>51 Grind:</span>{$grind51}</div>" : "";
?>
When there is data entered for the array if returns the information fine. Is there a way to not get an error if no information is entered? By the way, line 35 pertains to
$grind51 = join(", ", $_REQUEST["grind51"]);
in my code.
Just check whether $_REQUEST['grind51'] is set before you try to join it. The error is caused be the fact that an empty return isn't an array (and can't be joined).
Your PHP looks good, the HTML I would change: set the element names to just name[] in order to populate a regular (not associative) array.
<li data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose Grind:</legend>
<input type="checkbox" name="grind51[]" id="vgrind" class="custom" value="V-Grind">
<label for="vgrind">V-Grind</label>
<input type="checkbox" name="grind51[]" id="tourgrind" class="custom" value="Tour-Grind">
<label for="tourgrind">Tour Grind</label>
<input type="checkbox" name="grind51[]" id="healgrind" class="custom" value="Heal-Grind">
<label for="healgrind">Heal Grind</label>
<input type="checkbox" name="grind51[]" id="nogrind" class="custom" value="No Grind">
<label for="nogrind">No Grind</label>
</fieldset>
</li>
Do some quick error checking in PHP to be sure you have data in an array passed in:
if ( isset( $_REQUEST['grind51'] ) && is_array( $_REQUEST['grind51'] ) ) {
$grind51 = join(", ", $_REQUEST["grind51"]);
}
Related
I want the code below to show all the selected checkboxes and to have a limit of 2 checkboxes ticked.
Input
<form method="post" action="Outputofinfo.php">
<b style="font-size:19px ; color: #7a1ac4; font: Arial,tahoma,sans-serif; ">Favourite Movie Genre:</b><br>
<input type="checkbox" name="mg" value="Romance">
<label for="mg1"> Romance</label><br>
<input type="checkbox" name="mg" value="Comedy">
<label for="mg2"> Comedy</label><br>
<input type="checkbox" name="mg" value="Horror">
<label for="mg3"> Horror</label><br>
<input type="checkbox" name="mg" value="Action">
<label for="mg4"> Action</label><br>
<input type="checkbox" name="mg" value="Fiction">
<label for="mg5"> Fiction</label><br>
<input type="submit">
</form>
output
$fmg = $_POST['mg'];
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>Favourite Movie Genre:</b>
<?php echo $fmg; ?>
</div>
Adding [] to the end of name attribute like name=mg[] makes it send an array of values to the server. You can simply access the array on the server-side. For example you can access first element of it like: $_POST['mg'][0].
By using a foreach you can access all of the selected checkboxes.
Or you can use something like this:
$fmg = implode(", ", $_POST['mg']);
and show $fmg to the user as you want.
Your code will be:
<form method="post" action="Outputofinfo.php">
<b style="font-size:19px ; color: #7a1ac4; font: Arial,tahoma,sans-serif; ">Favourite Movie Genre:</b><br>
<input type="checkbox" name="mg[]" value="Romance">
<label for="mg1"> Romance</label><br>
<input type="checkbox" name="mg[]" value="Comedy">
<label for="mg2"> Comedy</label><br>
<input type="checkbox" name="mg[]" value="Horror">
<label for="mg3"> Horror</label><br>
<input type="checkbox" name="mg[]" value="Action">
<label for="mg4"> Action</label><br>
<input type="checkbox" name="mg[]" value="Fiction">
<label for="mg5"> Fiction</label><br>
<input type="submit">
</form>
output:
$fmg = implode(",",$_POST['mg']);
<div style ='font:21px Arial,tahoma,sans-serif;color:#7a1ac4'>
<b>Favourite Movie Genre:</b>
<?php echo $fmg; ?>
</div>
I have one dynamic checkbox (can be ++) that needs to stay checked after user submit.
I already tried some tricks like using a hidden input before my checkbox code in HTML. Now I stuck in doing an isset(_POST) and the checkbox didn't stay checked.
Here's my HTML:
<input type="hidden" name="hidden_name[]" id="hidden_name0">
<input type="checkbox" name ="name[]" id="name0">
<label for="name">Name</label>
--------UPDATE--------
And in my PHP file, here's the code:
$valueName = array();
if(isset($_POST["hidden_name"]))
{
foreach($_GET['hidden_name'] as $value)
{
array_push($valueName,$value);
}
}
That code doesn't work :/
How to make the checkbox stays checked after user check it and submit the form? What should I write in PHP? Do I really need a hidden input before my checkbox?
isset might be returning false since the submitted value is NULL. I suggest adding a value='1' on the hidden input field -- or are you updating that field with the corresponding checkbox value?
Alternatively, you have some other options:
Change the name for each dynamic checkbox to have an identifier.
<input type="checkbox" name="name-x" <?php echo isset( $_POST['name-x'] ) ? 'checked="checked"' : '' ?> />
Where x could be your dynamic ID. Notice the addition of the PHP code and using a ternary operator, you can check the checkbox if the $_POST['name-x'] is set.
Add a value to the checkbox.
<input type="checkbox" name="name[]" id="name0" value="name0" />
<input type="checkbox" name="name[]" id="name1" value="name1" />
<input type="checkbox" name="name[]" id="name2" value="name2" />
However, you need to match this value in your PHP code.
<?php
if ( isset( $_POST['name'] ) ) {
$values = array();
foreach( $_POST['name'] as $value ) {
array_push( $values, $value );
}
}
?>
Then you have to modify your checkbox again to have inline PHP.
<input type="checkbox" name="name[]" id="name0" value="name0" <?php echo in_array( "name0", $values ) ? 'checked="checked"' : '' ?> />
<input type="checkbox" name="name[]" id="name1" value="name1" <?php echo in_array( "name1", $values ) ? 'checked="checked"' : '' ?> />
<input type="checkbox" name="name[]" id="name2" value="name2" <?php echo in_array( "name2", $values ) ? 'checked="checked"' : '' ?> />
You can also create a function to display these inline codes to make it much cleaner. HTH!
i assume that you have a form that have some checkboxes and try to check which checkbox is checked. here is an example code
<form action="" method="post">
<label for="name">Name 1</label>
<input type="checkbox" name ="name[]" value="name1">
<label for="name">Name 2</label>
<input type="checkbox" name ="name[]" value="name2">
<label for="name">Name 3</label>
<input type="checkbox" name ="name[]" value="name3">
<label for="name">Name 4</label>
<input type="checkbox" name ="name[]" value="name4">
<button type="submit" name="submit">Submit</button>
</form>
and the php code
<?php
if(isset($_POST["submit"])){
$valueName = array();
foreach($_POST['name'] as $value){
array_push($valueName,$value);
print_r($valueName);
}
?>
I am in a silly position where I can't figure out to get the values of the checked checkboxes.
<form id="civilForm" method="POST" action="form.php" enctype="multipart/form-data">
<p>
<label>
<input id="12D" name="programsRequested[]" type="checkbox" />
<span>12D</span>
</label>
</p>
<p>
<label>
<input id="xp" name="programsRequested[]" type="checkbox" />
<span>XPStorm</span>
</label>
</p>
<p>
<label>
<input id="autoTurn" name="programsRequested[]" type="checkbox" />
<span>AutoTurn</span>
</label>
</p>
<p>
<label>
<input id="hecras" name="programsRequested[]" type="checkbox" />
<span>HEC RAS</span>
</label>
</p>
Then I am using a php loop as there are a bunch more checkboxes coming.
It spins through fine, but only give me a list that says: on, on, on which correctly tells me how many I checked, however does not give me the value of the checked box.
$selectedPrograms = 'None';
if(isset($_POST['programsRequested']) && is_array($_POST['programsRequested']) && count($_POST['programsRequested']) > 0){
$selectedPrograms = implode(', ', $_POST['programsRequested']);
}
Is there something obvious I missing on how to get the values here?
add every input element value
<input id="12D" name="programsRequested[]" type="checkbox" value="1" />
form not closed .
submit button also missing .
<input type="submit" name="submit" >
I can not get the form check boxes to come through to the email for "recycling". I do not know php so I have no idea what is wrong. When the form comes through to email the "Recycling:" subject is there, but the checked boxes are not.
HTML:
<label>CRT Monitors <input name="recycleobject2[]" type="checkbox" value="crtmonitors"
/></label>
<label>Printers <input name="recycleobject2[]" type="checkbox" value="printers"
/></label>
<label>Computers <input name="recycleobject2[]" type="checkbox" value="computers"
/></label>
<label>Fluorescent Lamps and Batteries <input name="recycleobject2[]" type="checkbox"
value="lamps" /></label>
<label>Televisions <input name="recycleobject2[]" type="checkbox" value="television"
/></label>
<label>Other Equipment <input name="recycleobject2[]" type="checkbox" value="other" />
</label><br />
PHP:
$recycleobject2 = $_POST['recycleobject2']; // not required
$recycleobject2 = array();
$email_message .= "Recycling:" .implode(", ",$recycleobject2)."\n";
your problem is right here:
$recycleobject2 = array();
That line is resetting the $recycleobject2 variable to a brand new empty array. Remove that line and your code should be fine.
I am writing a php site that has a form with a series of check boxes. I will be loading an array from a file that I would like to go through and check some of the boxes by default when the form is loaded.
Here is an example:
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" />
</form>
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
Is this possible? What would be the best way to do it.
You should fill your array before the HTML part. And then:
<input type="checkbox" name="option1" value="option1" <?php if (in_array("option1", $array)) { echo 'checked="checked"'; } />
Try this :
<?php
$array = array("option1", "option3");
// for loop to check boxes 1 and 3.
?>
<form action="mypage.php">
<label for="option1">Option 1</label>
<input type="checkbox" name="option1" value="option1" <?php if(in_array("option1",$array)){?> checked="checked"<?php}?> />
<label for="option2">Option 2</label>
<input type="checkbox" name="option2" value="option2" <?php if(in_array("option2",$array)){?> checked="checked"<?php}?> />
<label for="option3">Option 3</label>
<input type="checkbox" name="option3" value="option3" <?php if(in_array("option3",$array)){?> checked="checked"<?php}?> />
</form>