I'm looking for a way to have a simple form (yes and no radio button questions) where if the user answers yes to all of the questions and hits submit, a hidden link to a file is made visible. I'm no good at creating my own PHP yet...any suggestions?
<form name="myform" action="http://www.mydomain.com/myformhandler.php" method="POST">
<div align="center"><br>
<p>Question nubmer 1...</p><input type="radio" name="group1" value="Yes"> Yes<br>
<input type="radio" name="group1" value="No" checked> No<br>
<hr>
<p>Question nubmer 2...</p><input type="radio" name="group2" value="Yes"> Yes<br>
<input type="radio" name="group2" value="No"> No<br>
</div>
</form>
This would be visible if the answer to both questions is yes...
<div>
grab the file here
</div>
<?php
if (isset($_POST['group1']) && isset($_POST['group2'])) {
if ($_POST['group1']=='Yes' && $_POST['group2']=='Water') print '<div>grab the file here</div>';
}
?>
You basically just need to validate the values of the form that is being submitted.
$group1 = $_POST['group1'];
$group2 = $_POST['group2'];
if ($group1 == 'Yes' AND $group2 == 'Yes') echo 'My hidden data'[
You're interested in the GET/POST global arrays, in this case POST:
<form name="myform" action="" method="POST">
<div align="center"><br>
<p>Question nubmer 1...</p>
<input type="radio" name="group1" value="Yes"> Yes<br>
<input type="radio" name="group1" value="No" checked> No<br>
<hr>
<p>Question nubmer 2...</p>
<input type="radio" name="group2" value="Water"> Yes<br>
<input type="radio" name="group2" value="Beer"> No<br>
<input type="submit"/>
</div>
</form>
<?php
if ($_POST['group1'] == 'Yes' && $_POST['group2'] == 'Water') {
echo '<div>grab the file here</div>';
}
?>
Try it: http://jfcoder.com/test/grabfile.php
$group1 = $_POST('group1);
$group2 = $_POST('group2);
Then just use '==' (Equal or Set as) to check if they are the same:
if($group1 == 'Yes' && $group2 == 'Yes') print grab the file here';
The code above will print the file. The reason is that the values of the operands are equal.
I'm way late to this party but I thought the OP would like to know you could also do this in JavaScript without posting back to the page:
$('form[name="myform"]').submit(function(event){
event.preventDefault();
if($('input[name="group1"]').val() == "Yes" && $('input[name="group2"]').val() == "Water"){
$('#linkToPDF').show();
}
});
You can see it working here:
http://jsfiddle.net/ajp4r/
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"'; ?>
So Im having a bit of a conundrum. Not too sure how I should go about doing the if/else statements with an Echo at the top with the answers, with bullets.
<?php
echo
?>
</p>
<p>
A Chicken
<input type="radio" name="radio" id="radio3" value="no">
<label for="radio3">Choose this answer</label>
</p>
<p dir="ltr">A Ear of Corn.
<input type="radio" name="radio" id="radio2" value="no">
<label for="radio2">Choose this answer</label>
</p>
<p dir="ltr"> A Heart.
<input type="radio" name="radio" id="radio" value="yes"> Choose this answer
</p>
<p dir="ltr">
<input type="button" name="button28" id="button28" value="Yes! That is the answer I chose!">
If your question is basically about how to output certain HTML code based on an if/else clause, then with such a lengthy amount of HTML code as you have posted you would do much better using HEREDOCS checkout the PHP manual here: http://php.net/manual/en/language.types.string.php.
Goodluck!
You can do this on many ways. Here is a small example to get you started working with forms.
<?php
$answer = '';
if(isset($_POST['send']) //check if the form has been sent
&& isset($_POST['answer'])){ //check if an answer is filled in
//You could also use a switch -> http://php.net/manual/en/control-structures.switch.php
if($_POST['answer'] == 'chicken'){ //check if post variable answer value = chicken
$answer = 'your answer is "a chicken"';
}elseif($_POST['answer'] == 'corn'){ //check if post variable answer value= corn
$answer = 'your answer is "A Ear of Corn."';
}elseif($_POST['answer'] == 'heart'){ //check if post variable answer value = heart
$answer = 'your answer is "A Heart"';
}
//echo the variable.
echo $answer;
}
?>
<form method="POST" action="">
<p> A Chicken
<input type="radio" name="answer" id="radio3" value="chicken">
<label for="radio3">Choose this answer</label>
</p>
<p dir="ltr">A Ear of Corn.
<input type="radio" name="answer" id="radio2" value="corn">
<label for="radio2">Choose this answer</label>
</p>
<p dir="ltr">A Heart.
<input type="radio" name="answer" id="radio" value="heart">Choose this answer
</p>
<input type="submit" name="send" id="button28" value="Yes! That is the answer I chose!">
</form>
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.
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 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...
});