I have several groups of radio buttons that I want to use in an IF statement (or if you have a better solution)
Users will come to the site, select the buttons, then select submit. After submitting, I want the user to see instantly if they should "refer patient" or "don't refer patient".
I am not sure of a couple of things:
How do I make the "submit" button cause the input to be calculated (meaning, the user gets the instant response)
Since there are several combinations of inputs that can create a "refer" or "don't refer" response, can I add multiple conditions to the IF statement? Also, how can I include radio buttons in the statement - do I just use the "value" of the button. I only learned the very basic method of using numbers..
Below is my code so far. I tried to start the IF statement with values. Not sure if doing it right.
Any help is greatly appreciated!
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Indications for Mohs</title>
<?php
$Patient_status='unchecked';
$Primary_status='unchecked';
$Type_status='unchecked';
$BCCT_status='unchecked';
$SCCT_status='unchecked';
$Size_status='unchecked';
$Area_status='unchecked';
if (isset($_POST['Submit'])) {
$selected_radio=$_POST['REFER'];
if (selected_radio == "Healthy" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H" or "Immunocompromised" && "Primary" && "BCC" && "Aggressive" && "<0.6" && "H")
?>
</head>
<body>
<form name="Indications" action="" method="POST">
<p><h2><strong><u>Indications for Mohs Surgery</u></strong></h2>
</p>
<strong>Patient </strong>
<div alighn="center"><br>
<input type="radio" name="Patient" value="Healthy">Healthy<br>
<input type="radio" name="Patient" value="Immunocompromised">Immunocompromised<br>
<input type="radio" name="Patient" value="Genetic">Genetic Syndrome<br>
<hr>
<strong>Primary vs Recurrent</strong>
<div alighn="center"><br>
<input type="radio" name="Primary" value="Primary">Primary<br>
<input type="radio" name="Primary" value="Recurrent">Recurrent<br>
<hr>
<strong>Type</strong>
<div alighn="center"><br>
<input type="radio" name="Type" value="BCC">BCC<br>
<input type="radio" name="Type" value="SCC">SCC<br>
<input type="radio" name="Type" value="LM">LM or MIS<br>
<hr>
<strong>BCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="BCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="BCCT" value="Nodular">Nodular<br>
<input type="radio" name="BCCT" value="Superficial">Superficial<br>
<hr>
<strong>SCC subtype</strong>
<div alighn="center"><br>
<input type="radio" name="SCCT" value="Aggressive">Aggressive<br>
<input type="radio" name="SCCT" value="Nonaggressive">Nonaggressive<br>
<input type="radio" name="SCCT" value="Verrucous">Verrucous<br>
<input type="radio" name="SCCT" value="KA">KA - type SCC<br>
<input type="radio" name="SCCT" value="Bowen">In situ SCC/Bowen<br>
<input type="radio" name="SCCT" value="AK">AK<br>
<hr>
<strong>Size (cm)</strong>
<div alighn="center"><br>
<input type="radio" name="Size" value="0.5"><0.6<br>
<input type="radio" name="Size" value="0.6-1">0.6-1<br>
<input type="radio" name="Size" value="1.1-2">1.1-2<br>
<input type="radio" name="Size" value="2">>2<br>
<hr>
<strong>Area</strong>
<div alighn="center"><br>
<input type="radio" name="Area" value="H">H<br>
<input type="radio" name="Area" value="M">M<br>
<input type="radio" name="Area" value="L">L<br>
<hr>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
<p><strong><u>Definitions</u>:</strong><br>
Nonaggressive SCC: <2mm depth without other defining features, Clark level ≤III<br>
Area H: 'Mask Areas' of face (central face, eyelids, eyebrows, nose, lips [cutaneous/mucosal/vermillion], chin, ear, and periauricular skin/sulci, temple), genitalia (including perineal and perianal), hands, feet, nail units, ankles, nipples/areola<br>
Area M: Cheeks, forehead, scalp, neck, jawline, pretibial surface<br>
Area L: Trunk and extremities (excluding pretibial surface, hands, feet, nail units and ankles)</p>
</div>
</form>
</body>
</html>
If you want to have the submit display a response instantly you should use JavaScript as this does not require a form submission/call to a server. You can use the onsubmit event.
Regarding checking for if a radio button is checked, use the .checked property of an element:
document.getElementById('elem').checked //true or false
Almost everything is wrong with that code.
Let's start...
You don't have a REFER element in your form, so $_POST['REFER'] is never set. To access radio button values, you need to access with their relevant name as the index key to the $_POST[] array. E.g. $_POST['Patient'], $_POST['Primary'] ...etc. Those will give you the value of the radio button selected within that group.
Secondly, your conditional statements are wrong in the if statement. To compare conditional statements, you have to specifically compare the variable with different values every time. You'd have to say
if ($selected_radio == "Healthy" && $selected_radio == "Patient") {
// code goes here
}
And also, to check which radio button was selected, you need to access $_POST['<Radio_group_name>'] and this will give you the value of the radio button selected for that group. e.g.
$_POST['Patient']
would give Healthy if user selected that one for the group.
Related
I'm using a html form to edit sql db, thus I would want to have the form display the value currently in the database. I'm Using a while loop to display all the sql rows. I simple radio button allowing user to chose between 'Listings' (shows as '0' in sql) or 'Recent Transactions" (shown as '1' in sql).
The radio buttons are not pre-filling the value from sqlas checked or not
<form name="edit listing" action="edit_list.php" method="post" id="edit_form" >
<ul>
<?php while ($data=mysqli_fetch_assoc($result)):
$transaction = $data['transaction'];
$chkvalue='checked="checked"'; ?>
<li>
<fieldset>
<legend>Designation <h6>Required</h6></legend>
<input name="transaction" type="radio" tabindex="11" value="0" <?php if ($transaction == '0') echo $chkvalue; ?> />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" <?php if ($transaction == '1') echo $chkvalue; ?> />
<label for="recent_transactions">Recent Transactions</label>
</fieldset>
<input type="submit" formaction="edit_list.php" value="Submit Changes" />
</form>
</li>
<?php endwhile;mysqli_close($con);?>
</ul>
The short php insert is working, a little. My source code is looking something like this:
<input name="transaction" type="radio" tabindex="11" value="0" />
<label for="listings">Listings</label>
<input name="transaction" type="radio" tabindex="12" value="1" checked="checked" />
<label for="recent_transactions">Recent Transactions</label>
But the radio button is not pre-filling.
I've been poking at this all day, any suggestion here where problem could be would be extremely helpful
You only have to write checked not check="checked"!
So change it to this:
$chkvalue='checked'; ?>
instead of this:
$chkvalue='checked="checked"'; ?>
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
this is one radio button set
<input type="radio" name="image" id="100" value="a"/>
<input type="radio" name="image" id="100" value="b"/>
<input type="radio" name="image" id="100" value="c"/>
<input type="radio" name="image" id="200" value="d"/>
<input type="radio" name="image" id="200" value="e"/>
<input type="radio" name="image" id="300" value="f"/>
<input type="radio" name="image" id="300" value="g"/>
<input type="radio" name="image" id="400" value="h"/>
and so on...
and this is another radio button set
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>200
<input type="radio" name="number" value="300"/>300
<input type="radio" name="number" value="400"/>400
<input type="radio" name="number" value="500"/>500
<input type="radio" name="number" value="600"/>600
my question is how can i compare value of one with value="100" to other with id="100" in php
there is a comparison i want to make but for the field with id = 100 i have values as abcd.. so i cant assign it value = 100 cuz i m saving it in database
means..
there are other fields with id=100 but their values shall be diffrent otherwise i m not able to identify them after they are send to databse
still i have to do comparison of both.. and that too serverside
so what are my options?
more detailed explanation....
there are two sets of radio buttons..
in one set there are values..
and in other sets there are images related to those values..
when user clicks set of value... and user click on set of images
so php shall match that image to the value.. if user clicked on 100 as value and later he clicks on an image under the id 200
database shall not allow it to enter and gave him some error
Sorry Had to see what your talking about. Help to edit this so to understand your requirement, 'shall match that image to the value'
<input type="radio" name="image" class="100" value="a_100"/>
<input type="radio" name="image" class="100" value="b_100"/>
<input type="radio" name="image" class="100" value="c_100"/>
<br>
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>
<input type="radio" name="number" value="300"/>
<?php
$image = strstr($_POST['image'],'_');
$number = '_'.$_POST['number'];
if($image == $number) {
//Do something
}
The question is looking for to compare the two fields in PHP. Presumably this means that you want to examine them when the form is posted.
This means that you'll only have available to you the parts of the field that are sent via the POST -- ie the field name and the value. PHP will never see the id of any of the fields, nor the class, nor any other attributes in the HTML code other than name and value.
Therefore the discussion in the comments about whether or not to use id is a somewhat moot point -- even if it is a good point for you to know in general for your HTML code, you can't really use either id or class here anyway because PHP will never see them.
You need to take an entirely different approach.
[EDITED after question edit]
Add the 100 or 200, etc to the value of the image radio buttons, like so:
<input type="radio" name="image" value="a_100"/>
<input type="radio" name="image" value="b_100"/>
<input type="radio" name="image" value="c_100"/>
<input type="radio" name="image" value="d_200"/>
<input type="radio" name="image" value="e_200"/>
<input type="radio" name="image" value="f_300"/>
<input type="radio" name="image" value="g_300"/>
<input type="radio" name="image" value="h_400"/>
(The number radio button set remains unchanged from your question)
You can then read it in PHP as follows:
$number = $_POST['number'];
list($imgNumber, $imgValue) = explode('_',$_POST['image']);
You can now compare $number with $imgNumber.
Hope that helps.
It's worth asking a follow-up question though: What are you trying to achieve here? Is this a validation exersise? ie where you're checking that the user is selecting an image option that matches the number option he's picked? If that's what you're doing, you may find it's more user friendly to use Javascript to filter the options available when they select the number option so that the image radio button set only shows options that are valid for the selected number.
This would be an entirely different question, so I won't go into any detail here, but I would suggest that it might be a more user-friendly way of doing things if you did it like that.
I try to implement the follwing code:
<?php
$yesno1="";
$yesno2="";
$option1="";
$option2="";
$option3="";
if(isset($_POST['submit'])){
$yesno1=$_POST['yesno1'];
$yesno2=$_POST['yesno2'];
$option1=$_POST['option1'];
$option2=$_POST['option2'];
$option3=$_POST['option3'];
}
?>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<input type="radio" name="yesno1" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno1" value="no" style="outline:0;"/>No
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="outline:0;"/>No
<input type="checkbox" name="option1" value="Milk"> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese"> Cheese<br>
<input type="submit" value="Submit" name='submit'>
</form>
Once I click submit button, normally the value I seleted in radio and marked in the checkbox are gone, so how can I keep the value even after the click the submit button or refrsh the page using php, javascript is fine as well, would be better if any one can help me with it on both php and javascript since I am not very good on both of them, thanks for kind help:)
An example below
<input type="checkbox" name="option1" value="Milk" <?php echo ($_POST['option1'] == 'Milk' ? 'checked' : '') ?>> Milk<br>
From MDN:
The input (<input>) element is used to create interactive controls for
web-based forms.
Attributes
type
[...]
radio: A radio button. You must use the value attribute to define the
value submitted by this item. Use the checked attribute to indicate
whether this item is selected by default. Radio buttons that have the
same value for the name attribute are in the same "radio button
group"; only one radio button in a group can be selected at one time.
This is a basic HTML question. Learn to walk before you run ;-)
A very basic question...
How can I only allow the selection of one option in a list of radio buttons?
<form action="process_repair.php" method="POST">
<label for "repair_complete">Repair complete</label>
<input type="radio" name="Yes" value="true">
<input type="radio" name="No" value="false">
</form>
When this code runs, it's possible to select both radio buttons, but I'd like them to interact so you can only select one or the other.
Any help much appreciated! :)
Give them the same name.
<form action="process_repair.php" method="POST">
Repair complete
<input type="radio" name="complete" value="true" id="complete_yes" />
<label for="complete_yes">Yes</label>
<input type="radio" name="complete" value="false" id="complete_no" />
<label for="complete_no">No</label>
</form>
Labels must have a for attribute, directing to the corresponding input's id.
Every input should have same "name"