Checkbox value match, but now show checked in checkbox - php

Is there something wrong with my code :
if($q_ccd_chk == 'on')
{
if($q_front == 1)
{
echo '<input type="checkbox" name="front" value="1" checked/> Front';
}
if($q_back == 1)
{
echo '<input type="checkbox" name="back" value="1" checked/> Back';
}
if($q_fb == 1)
{
echo '<input type="checkbox" name="fb" value="1" checked/> FB';
}
}
else
{
echo '<input type="checkbox" name="front" value="1"/> Front';
echo '<input type="checkbox" name="back" value="1"/> Back';
echo '<input type="checkbox" name="fb" value="1"/> FB';
}
If value checkbox front and back value match, it will show checkbox with checked. But if the 3 of checkbox value match, its not show checked for all checkbox.

You only check if $q_ccd_chk is on. And if it is not you show all the unchecked values. You should check them ALL before you can do this. If $q_ccd_chk is on but the other 3 are not. It shows no checkboxes at all...

What do you get with :
$checkedfront = "";
$checkedback = "";
$checkedfb = "";
if($q_ccd_chk == 'on')
{
if($q_front == 1) { $checkedfront = " checked";}
if($q_back == 1) { $checkedback = " checked";}
if($q_fb == 1) { $checkedfb = " checked";}
}
echo "<input type=\"checkbox\" name=\"front\" value=\"1\"".$checkedfront." /> Front\n";
echo "<input type=\"checkbox\" name=\"back\" value=\"1\"".$checkedback." /> Back\n";
echo "<input type=\"checkbox\" name=\"fb\" value=\"1\"".$checkedfb." /> FB\n";

Related

default radio buttons are getting selected PHP

I am new to php and MYSQL. For Morrocan oil, the radio buttons are getting checked when I click on Yes or No. But, the printStatement when I click is Yes/No. The default No is getting checked no matter what. Can you please help me resolve this issue. Thanks!
echo '<tr>
<td>' . _('Signed Moroccan Oil?') . '</td>';
if(strtoupper($_POST['signedMor']) == 'YES'){
echo '<td> <input type="radio" name="signedMor" id="signedMor" value="Yes" checked="checked">Yes
<input type="radio" name="signedMor" id="signedMor" value="No">No</td>';
} else {
echo '<td> <input type="radio" name="signedMor" id="signedMor" value="Yes">Yes
<input type="radio" name="signedMor" id="signedMor" value="No" checked="checked">No</td>';
}
echo '</tr>';
echo '<tr>
<td>' . _('Print Statement?') . '</td>';
if(strtoupper($_POST['printStatement']) == 'Y'){
echo '<td> <input type="radio" name="printStatement" id="printStatement" value="Y" checked="checked">Yes
<input type="radio" name="printStatement" id="printStatement" value="N">No</td>';
} else {
echo '<td> <input type="radio" name="printStatement" id="printStatement" value="Y">Yes
<input type="radio" name="printStatement" id="printStatement" value="N" checked="checked">No</td>';
}
echo '</tr>';

preg_match not working correctly?

I have recently updated one of my php files to allow for a new preg_match but it seems to work only in the first two instances
//Normal Values Check
if(preg_match("/norm/i", $drop) && $ruavalue === "0" || $ruavalue === "2" || $ruavalue === "4" || $ruavalue === "6")
{
echo '<form action="" method="post">';
echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
echo '<input type="submit" name="ruasubmit" value="RUA!" />';
echo '</form>';
}
//Normal Values Achieved Then Inform User
elseif(preg_match("/norm/i", $drop) && $ruavalue === "1" || $ruavalue === "3" || $ruavalue === "5" || $ruavalue === "7") {
echo "You Have RUA'ed To This Boss";
}
//Heroic Values Check
elseif(preg_match("/hc/i", $drop) && $ruavalue === "0" || $ruavalue === "1" || $ruavalue === "4" || $ruavalue === "5")
{
echo '<form action="" method="post">';
echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
echo '<input type="submit" name="ruasubmit" value="RUA!" />';
echo '</form>';
}
//Heroic Values Achieved Then Inform User
elseif(preg_match("/hc/i", $drop) && $ruavalue === "2" || $ruavalue === "3" || $ruavalue === "6" || $ruavalue === "7") {
echo "You Have RUA'ed To This Boss";
}
//Mythic Values Check
elseif(preg_match("/myth/i", $drop) && $ruavalue === "0" || $ruavalue === "1" || $ruavalue === "2" || $ruavalue === "3")
{
echo '<form action="" method="post">';
echo "<input name=\"drop1\" type=hidden value='".$drop."'>";
echo "<input name=\"ruavalue1\" type=hidden value='".$ruavalue."'>";
echo "<input name=\"boss\" type=hidden value='".$_POST['tier_two']."'>";
echo "<input name=\"main\" type=hidden value='".$maintoonqry3."'>";
echo '<input name="the_page" type=hidden value="RUA/rua-system.php">';
echo '<input type="submit" name="ruasubmit" value="RUA!" />';
echo '</form>';
}
//Mythic Values Achieved Then Inform User
elseif(preg_match("/myth/i", $drop) && $ruavalue === "4" || $ruavalue === "5" || $ruavalue === "6" || $ruavalue === "7") {
echo "You Have RUA'ed To This Boss";
}
The issue im having is that my 1st two button's will display fine but when my $ruavalue = 3 then my 3rd button will not work i dont know if im just being blind to the issue or if its particularly bad code
I think I like leaving those array values as strings, even though they're integers, just because of how my notepad++ colors them with syntax highlighting (text is grey, numbers are red, I like grey). You could do it either way, though.
<?php
$_POST['tier_two'] = 'tier_two';
$drop = 'myth';
$ruavalue = '9';
$tier_two = $_POST['tier_two'];
$maintoonqry3 = 'ebola!';
// -----------------------------------------
$vals = array('drop'=>$drop,
'ruavalue'=>$ruavalue,
'tier_two'=>$tier_two,
'maintoonqry3'=>$maintoonqry3);
$test = array('norm'=>array('0','2','4','6'),
'hc' =>array('0','1','4','5'),
'myth'=>array('0','1','2','3'));
foreach ($test as $key => $array) {
if (preg_match("/$key/i", $drop)) {
if (in_array($ruavalue,$array)) {
display_my_form($vals);
}
else echo 'You Have RUA\'ed To This Boss';
break; // put this here if you only want to hit
// the first key (norm,hc,myth) match,
// determine valid or not, an d then stop.
}
}
function display_my_form($vals) {
echo "
<form action='' method='post'>
<input name='drop1' type='hidden' value='{$vals['drop']}'>
<input name='ruavalue1' type='hidden' value='{$vals['ruavalue']}'>
<input name='boss' type='hidden' value='{$vals['tier_two']}'>
<input name='main' type='hidden' value='{$vals['maintoonqry3']}'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
";
}
?>
wow....
k, so... i dunno. i tested all regex and good/bad values for it all (both sections of code work). not sure why you have those input fields hidden but that's alright.
<?php
$_POST['tier_two'] = 'tier_two'; // for testing
$drop = 'myth';
$ruavalue = '5';
$tier_two = $_POST['tier_two'];
$maintoonqry3 = 'ebola!';
$vals = array('drop'=>$drop,'ruavalue'=>$ruavalue,
'tier_two'=>$tier_two,'maintoonqry3'=>$maintoonqry3);
if (preg_match('/norm/i', $drop)) {
if (in_array($ruavalue,array('0','2','4','6'))) {
display_my_form($vals);
}
else echo_error();
}
if (preg_match('/hc/i', $drop)) {
if (in_array($ruavalue,array('0','1','4','5'))) {
display_my_form($vals);
}
else echo_error();
}
if (preg_match('/myth/i', $drop)) {
if (in_array($ruavalue,array('0','1','2','3'))) {
display_my_form($vals);
}
else echo_error();
}
function display_my_form($vals) {?>
<form action='' method='post'>
<input name='drop1' type='hidden' value='<?php echo $vals['drop'] ?>'>
<input name='ruavalue' type='hidden' value='<?php echo $vals['ruavalue'] ?>'>
<input name='boss' type='hidden' value='<?php echo $vals['tier_two'] ?>'>
<input name='main' type='hidden' value='<?php echo $vals['maintoonqry3'] ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
}
function echo_error() {
echo 'You Have RUA\'ed To This Boss';
}
?>
and if you want to keep it your way... this also functions:
<?php
$_POST['tier_two'] = 'tier_two';
$drop = 'myth';
$ruavalue = '5';
$tier_two = $_POST['tier_two'];
$maintoonqry3 = 'ebola!';
if (preg_match('/norm/i', $drop) && in_array($ruavalue,array('0','2','4','6'))) { ?>
<form action='' method='post'>
<input name="drop1" type='hidden' value='<?php echo $drop ?>'>
<input name="ruavalue1" type='hidden' value='<?php echo $ruavalue ?>'>
<input name="boss" type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
<input name="main" type='hidden' value='<?php echo $maintoonqry3 ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
} elseif (preg_match("/norm/i", $drop) && in_array($ruavalue,array('1','3','5','7'))) {
echo "You Have RUA'ed To This Boss";
} elseif (preg_match("/hc/i", $drop) && in_array($ruavalue,array('0','1','4','5'))) { ?>
<form action="" method="post">
<input name='drop1' type='hidden' value='<?php echo $drop ?>'>
<input name='ruavalue1' type='hidden' value='<?php echo $ruavalue ?>'>
<input name='boss' type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
<input name='main' type='hidden' value='<?php echo $maintoonqry3 ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
} elseif (preg_match("/hc/i", $drop) && in_array($ruavalue,array('2','3','6','7'))) {
echo "You Have RUA'ed To This Boss";
} elseif (preg_match("/myth/i", $drop) && in_array($ruavalue,array('0','1','2','3'))) { ?>
<form action='' method='post'>
<input name='drop1' type='hidden' value='<?php echo $drop ?>'>
<input name='ruavalue1' type='hidden' value='<?php echo $ruavalue ?>'>
<input name='boss' type='hidden' value='<?php echo $_POST['tier_two'] ?>'>
<input name='main' type='hidden' value='<?php echo $maintoonqry3 ?>'>
<input name='the_page' type='hidden' value='RUA/rua-system.php'>
<input name='ruasubmit' type='submit' value='RUA!' />
</form>
<?php
} elseif (preg_match('/myth/i', $drop) && in_array($ruavalue,array('4','5','6','7'))) {
echo 'You Have RUA\'ed To This Boss';
}
?>

How do you echo out radio input types?

I have an html form that echos back text and radio inputs. The form waits for all values to be filled before echoing them back out.
Here is an example of my text input:
<input type='text' name='occ3' size='20' value="<?php echo $occ3; ?>"/>
Here is my radio output:
<input type='radio' name='o2' value='yes'>Yes
<input type='radio' name='o2' value='no'>No
How do I output the selected radio after a user submits information? Please help
If you want the radio buttons to default to what was submitted, you can do:
$o2 = $_REQUEST['o2'];
?>
<input type='radio' name='o2' value='yes' <?php if($o2 == 'yes') echo 'checked' ?> >Yes
<input type='radio' name='o2' value='no' <?php if($o2 == 'no') echo 'checked' ?> >No
php
if (isset($_POST['radio'])) { // if options was checked
echo $_POST['radio']; // echo the choice
else
echo "you nothing was selected.";
<input type='radio' name='o2' value='yes' <?php echo $o2 == 'yes' ? 'checked' : ''; ?>>Yes
<input type='radio' name='o2' value='no' <?php echo $o2 == 'no' ? 'checked' : ''; ?>>No

how to display value of the checked checkboxes?

$section = mysql_query("SELECT services.services, services.price FROM services WHERE pet_breed = 'Dog'");
while ($row = mysql_fetch_array($section))
{
echo "<br><input type='checkbox' id=check data-price='".$row['price']."' class='service' name='pet[]' value ='".$row['services']."'";
echo " />";
echo $row['services'];
}
you should use "checked" property in case you have data. for more please see this
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_checked
<input type="checkbox" name="vehicle" value="Car" checked> I have a car

Storing form data using html & php

I've got a form with 50 questions. After the user has filled in the form, I want to take them to another page to first cancel / confirm that they are finished with the form. My question is if they select cancel, how do I put back all the fields that has been answered in the form?
EDITED
I've edited my question to show my code because I'm strugling:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND()";
$result1=mysql_query($sql1);
echo "<form method='post' action='....'>";
while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "$question";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Finish'>";
echo "</form>";
Make the Cancel button a Submit and let it post back to the original questionnaire. In there, you can do like this:
<input type="text" name="q1" value="<?=isset($_POST['q1']) ? $_POST['q1'] : '';?>" />
Or:
<input type="radio" name="q2" value="A" <?=isset($_POST['q2']) && $_POST['q2'] == 'A' ? 'checked="checked"' : '';?>" />
<input type="radio" name="q2" value="B" <?=isset($_POST['q2']) && $_POST['q2'] == 'B' ? 'checked="checked"' : '';?>" />
Same goes for option elements inside a select (selected="selected").
Pass the $_POST or $_GET array to the confirmation page, and if they hit cancel, pass the array back to the page, and fill in the elements.
The easiest solution would be adding:
<INPUT type=button value="Cancel" onClick="history.back();">
When you go back, the form would still be there.
You can just use $_REQUEST['Answer'] in value tag of form.

Categories