I have a web form that updates a customer record. The entries are prefilled with the data stored in mysql when called. One field shows if equipment was returned as follows:
<font size=5>Returned:</font><input type="text" name="ud_Returned" value="<?php echo $rtrnd; ?>" /><br />
My question is I setup to convert this to radio button like:
<font size=5>Returned:</font><input type="radio" name="ud_Returned" value=Yes /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />
But I want it so the value stored in the $rtrnd variable fills in the existing radio button with the appropriate Yes/No for that customer when form is called. Any ideas?
Maybe something like this would do the trick:
<input type="radio" name="ud_Returned" value="Yes" <?php if($rtrnd) echo 'checked'; ?> />
<input type="radio" name="ud_Returned" value="No" <?php if(!$rtrnd) echo 'checked'; ?> />
Assuming that $rtrnd is a boolean value. If it isn't just use a comparison in the if statements like if($rtrnd == 'yes').
Assuming your store this information as 1 or 0 in the database you'll have to -
<?php
// Get stuff from database
$result = /* The resulting array from your query */;
if( $result["ud_Returned"] == 1 )
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" checked="checked" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" /> No<br />');
}
else
{
echo('<font size=5>Returned:</font>
<input type="radio" name="ud_Returned" value="Yes" /> Yes<br />
<input type="radio" name="ud_Returned" value="No" checked="checked" /> No<br />');
}
I'm not suggesting that this is the exact implementation you should pursue but this is the logic - get information from database, make a comparison, add the checked attribute to the appropriate radio button.
Related
I am working on a custom survey that is dynamically populated with database information and needs to be saved as an array. I can make everything work as it should except I would like to use a star rating system for many of the answers and it seems radio buttons won't work like this:
<input type="radio" name="surveyRating[]" value="1" />
<input type="radio" name="surveyRating[]" value="2" />
<input type="radio" name="surveyRating[]" value="3" />
<input type="radio" name="surveyRating[]" value="4" />
<input type="radio" name="surveyRating[]" value="5" />
Because there are multiple instances of this same code. This will be saved into an array so surveyRating will be used over and over again. If I click a radio button in one group, it changes to a radio in another group.
I have read I can possibly do this with checkboxes instead. Is that the best alternative? Or is there another option I should be looking at? I want the final product to be star ratings 1-5. Not sure if I can do that with checkboxes.
EDIT
Ok so using name="surveyRating[1]" is helping as far as the radio buttons not conflicting on the client side. However, it is not saving correctly the way I have my php set up now. Here is how it is saved currently. What do I need to change to make the [1] work appropriately. Currently it is only saving the last iteration.
$new = array();
$ratings = $_POST['surveyRating'];
$count = count( $ratings );
for ( $i = 0; $i < $count; $i++ ) {
if ( $ratings[$i] != '' ) {
$new[$i]['surveyRating'] = stripslashes( strip_tags( $ratings[$i] ) );
}
}
EDIT 2
So to demonstrate how I accomplished this using the answer below, I had to add [0] as the first iteration in the loop. I was using $items = 0; $items++ to dynamically add a number to each loop.
To make it start at 0, I set $items = -1 that way the first iteration is 0 instead of 1. Hope that makes sense.
Yep, it does because you have the name with []. That's for array. Remove it and you will get it without array:
<input type="radio" name="surveyRating" value="1" />
<input type="radio" name="surveyRating" value="2" />
<input type="radio" name="surveyRating" value="3" />
<input type="radio" name="surveyRating" value="4" />
<input type="radio" name="surveyRating" value="5" />
For radio buttons with several groups, you have to do something like this:
<!-- Group 1 -->
<input type="radio" name="surveyRating[1]" value="1" />
<input type="radio" name="surveyRating[1]" value="2" />
<input type="radio" name="surveyRating[1]" value="3" />
<input type="radio" name="surveyRating[1]" value="4" />
<input type="radio" name="surveyRating[1]" value="5" />
<!-- Group 2 -->
<input type="radio" name="surveyRating[2]" value="1" />
<input type="radio" name="surveyRating[2]" value="2" />
<input type="radio" name="surveyRating[2]" value="3" />
<input type="radio" name="surveyRating[2]" value="4" />
<input type="radio" name="surveyRating[2]" value="5" />
Okay so I have a script which is basically like an order form. However my only input options are radio buttons. When I run my script, I noticed that it turned back errors. Instead I would like it to display and empty field. The page posts back to itself and it updates a box I left blank on the bottom before submission. After submission details are filled in from a multitude. I also used a two dimensional array if that makes any difference. So my problems are with empty variables in the forms. I was not able to fix this and I think it might be the way I'm using it. I get an undefined index error for the variables that receive the data. In this case $settype and $differentype come out as errors since they are defined to be whatever was in the textbox. I thought that by using empty() I could eliminate that issue. I did not approach it correctly of course. Can anyone give me some guidance here? Thank you.
<form name="form1" method="POST" action="order.php">
<input type="radio" name="set" value="1" /> 1
<input type="radio" name="set" value="2" /> 2
<input type="radio" name="set" value="3" /> 3
<input type="radio" name="different" value="a" /> a
<input type="radio" name="different" value="b" />
<input type="radio" name="different" value="c" />
<input type="submit" name="submit1" value="login" />
</form>
<?php
if(isset($_POST['submit'])) {
$settype = $_POST['set'];
$differentype = $_POST['different'];
if ($settype == '1' && $differenttype =='a'){
$order = $field[0][1];
}
if (empty($settype) && empty($differenttype)){
$order = "";
}
}
?>
<table>
<tr>
<td>
<?php print($order); ?>
</td>
</tr>
</table>
Where is your input submit ? like this <input type="submit" name="submit" value="submit" />
EDIT 1: You should have a default radio button selected value like
<input type="radio" name="set" value="1" checked="checked"/> 1
<input type="radio" name="different" value="a" checked="checked" /> a
I have 3 checkboxes :
<input class="shoesChoice" name="trendy" type="checkbox" />
<input class="shoesChoice" name="luxury" type="checkbox" />
<input class="shoesChoice" name="sports" type="checkbox" />
And I want to store this information into (if possible) only one field in my database.
So, if the 1rst and the last input are checked, my shoesChoice field should have : "trendy, sports"
Try to change the name and keep them in value
<input class="shoesChoice" name="shoes[]" value="trendy" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="luxury" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="sports" type="checkbox" />
In PHP, try to get values in this way:
<?php
if(isset($_REQUEST['shoes']))
{
$shoes = '';
foreach
}
?>
I need a form in which the checkboxes would open different pages based on their selection when the form is submitted.
So, say I have this simple form:
<form action="" method="post">
<input value="1" type="checkbox" name="sign" />
<input value="2" type="checkbox" name="sign" />
<input value="3" type="checkbox" name="sign" />
<input value="4" type="checkbox" name="sign" />
<input value="5" type="checkbox" name="sign" />
<input type="submit" />
</form>
When an user checks value 1 and submits, he will be redirected to page A. If a user checks 1 and 4 he will be redirected to a different page (page F, for instance). If a user checks 2, 3 and 4 he will be redirected to page R, and so on... There would be 25 different combinations, and therefore, 25 different page results for this form when an user submits it.
In other words, when the form is submitted somehow the system would read which checkboxes were checked and associate each possible combination with a different URL.
Can it be done? If so, how? Anyone ever made something similar? I've searched a long time for solutions, but found only slightly similar ones, not exactly what I need, so any help would be appreciated.
HTML:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[1]" />
<input value="2" type="checkbox" name="sign[2]" />
<input value="3" type="checkbox" name="sign[3]" />
<input value="4" type="checkbox" name="sign[4]" />
<input value="5" type="checkbox" name="sign[5]" />
<input type="submit" />
</form>
PHP:
if (isset($_POST['sign'][1]))
header("Location: a.php");
elseif(isset($_POST['sign'][2]) AND isset($_POST['sign'][3]))
header("Location: b.php");
This can be done in a couple of ways. One is that you can use javascript to intercept the form submission, change the value of the form "action", and then execute the submit.
A second approach might be to just send all this data to a single script and based on the selected values perform a redirect to the intended page.
From should look like:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
And the post like:
if(!empty($_POST['sign'])) {
if(in_array(1, $_POST['sign'])) {
// if just 1, go to page
} elseif(in_array(1, $_POST['sign']) && in_array(4, $_POST['sign'])) {
// if 1 and 4, go to page
}
}
You can just consider each of the inputs as a bit. So, you will always have 5 bits to consider.
Then define an associative array of 25 entries corresponding to each of the possible values:
00001
00010
...
var links = {
"00001" : "www.google.com",
];
Then, when you submit the form, just set the target attribute of the form based on the value.
If I were you and I had no choice than following this idea, I would use something like that in jQuery (assuming your post vars are treated in the page you want to reach):
<form action="" method="post" id="myForm">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
$("#myForm").submit(function() {
var checked_array = new Array();
$("#myForm input").each(function() {
if ($(this).is(":checked")
checked_array.push($(this).attr("value"));
});
if ( checked_array.indexOf(2) !== -1 && checked_array.indexOf(5) !== -1)
("#myForm").attr("action", "/url1.php") ;
else if etc...
});
I have a page of questions that have two options as radio buttons.Each Question has a form tag.Now i want to store the values checked by the user to a database through a single function in php.How to do it?
My html code is as follows:
<li>Question1 </li>
<br /><form>A.
<input type="radio" name="radio" id="1ARadioButton" value="1ARadioButton" />
<label for="1ARadioButton">Answer1</label><br /><br />
B.
<input type="radio" name="radio" id="1BRadioButton" value="1BRadioButton" />
<label for="1BRadioButton"> Answer2</label>
</form><br /><br />
<li>Question2</li>
<br /><form>A.
<input type="radio" name="radio" id="2ARadioButton" value="2ARadioButton" />
<label for="2ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="2BRadioButton" value="2BRadioButton" />
<label for="2BRadioButton">Answer2</label>
</form><br /><br />
<li>Question3</li>
<br /><form>A.
<input type="radio" name="radio" id="3ARadioButton" value="3ARadioButton" />
<label for="3ARadioButton">Answer1</label>
<br /><br />
B.
<input type="radio" name="radio" id="3BRadioButton" value="3BRadioButton" />
<label for="3BRadioButton">Answer2</label>
</form><br /><br />
You've got the same name on all your radio buttons, so you're only going to get the value of the LAST radio button that's checked off. You're going to be recording the same answer for ALL questions (which is usually C, right?).
element IDs are NOT used for form submissions. They're used only for DOM operations. On HTML forms, only the name and value type attributes are relevant for the form submission process.
What you should have is:
Question 1:
<input type="radio" name="question1" value="option_A" />
<input type="radio" name="question1" value="option_B" />
Question 2:
<input type="radio" name="question2" value="option_A" />
<input type="radio" name="question2" value="option_B" />
etc...
As for storing them in the database, that's the same as storing any other form-data in a database. Get the radio's value with $_POST['question1'] or whatever, and do the usual escaping/query building/inserting.