I have a simple survey form i designed but for some reason one line of code isnt working.
In question 5. If the user selected the radio button YES then they must enter something in the textarea or the form must not be sent.
If no then the textarea should be locked so nothing can be entered.
Here is the code: Thanks for the help ladies and gentleman
5. Are you using other non-franchise service centres?
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5"
<?php if (isset($question5) && $question5=="Yes") echo "checked";?>
value="Yes">Yes
<br>
<input type="radio" name="question5"
<?php if (isset($question5) && $question5=="No") echo "checked";?>
value="No">No
<br>
<textarea name="question5" rows="5" cols="40"><?php echo $question5 </textarea>
Option1: hide the textarea
function check5(selectedType){
if (selectedType == 'Yes'){
document.getElementById('5textarea').style.display = 'block';
} else{
document.getElementById('5textarea').style.display = 'none';
}
}
5. Are you using other non-franchise service centres?
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5" value="Yes" onclick="check5(this.value)"> Yes
<br>
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No
<br>
<textarea id="5textarea" name="question5" rows="5" cols="40" style="display:none"></textarea>
Option2: enable/disable the textarea (NOT TESTED):
5. Are you using other non-franchise service centres?
<br>
*if yes is there any other reason you would do so other than price
<br>
<input type="radio" name="question5" value="Yes" onclick="check5(this.value)"> Yes
<br>
<input type="radio" name="question5" value="No" onclick="check5(this.value)"> No
<br>
<textarea id="5textarea" name="question5" rows="5" cols="40" disabled="true"></textarea>
and change the javascript to:
function check5(selectedType){
if (selectedType == 'Yes'){
document.getElementById("5textarea").disabled = false;
} else{
document.getElementById("5textarea").disabled = true;
}
}
You have to do it with javascript since PHP only runs on the server, not in the browser!
In this code, the textarea is disabled if not needed, and if "Yes" is checked and the textarea doesn't contain enough text, an error appears when submitting the form:
// Javascript:
function validate() {
var radioYes = document.getElementById('radioYes');
var textarea = document.getElementById('textarea');
if (radioYes.checked && textarea.value.length < 10) {
alert('Enter at least 10 characters!');
textarea.focus();
return false;
}
}
function toggle(value) {
document.getElementById('textarea').disabled = value;
}
/* CSS: */
* {
font-family: sans-serif;
margin: 4px;
}
<!-- HTML/PHP: -->
<form action="#">
5. Are you using other non-franchise service centres?<br>
*if yes is there any other reason you would do so other than price?<br>
<input type="radio" name="question5" value="Yes" onchange="toggle(this.selected)" id="radioYes">Yes<br>
<input type="radio" name="question5" value="No" onchange="toggle(!this.selected)">No<br>
<textarea id="textarea" name="question5" rows="5" cols="40" disabled></textarea><br>
<input type="submit" value="Send" onclick="return validate()" />
</form>
JSFiddle
Related
i made a simple calculator.But the problem is not radio buttons are not working properly to select the operation to be performed my code for input is as follow:
<form action="output.php" method="post" class="form">
<label for="">Value 1</label>
<input type="text" name="value1" placeholder="Enter 1st value">
<br>
<label for="">Value 2</label>
<input type="text" name="value2" placeholder="Enter 2nd value">
<h1>Select Operator </h1>
<input type="radio" name="addition" value="add">+
<br>
<input type="radio" name="subtraction" value="sub">-
<br>
<input type="radio" name="multiplication" value="mul">*
<br>
<input type="radio" name="division" value="div">/
<br>
<input type="submit" class="btn btn-success" value="Show Result">
</form>
and output php code is as follow:
<?php
$value_1=$_POST['value1'];
$value_2=$_POST['value2'];
if(isset($_POST['submit'])) {
if($_POST['operation'] == add) {
echo "$value_1 + $value_2";
}else if($_POST['operation'] == sub){
echo "$value_1 - $value_2";
}else if($_POST['operation'] == mul){
echo "$value_1 * $value_2";
}else if($_POST['operation'] == div){
echo "$value_1 / $value_2";
}
}
?>
The radio buttons need to share the same name, only the value should change.
label {
display: block;
}
<label><input type="radio" name="operation" value="add">+</label>
<label><input type="radio" name="operation" value="sub">-</label>
<label><input type="radio" name="operation" value="mul">*</label>
<label><input type="radio" name="operation" value="div">/</label>
The submit button has no name - so it will not appear in the POST array and the logic will never be processed. Rather than test for the presence ( or not ) of a button in the POST array you really should test for the actual form elements that you will use in your calculations.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>simple calculator</title>
</head>
<body>
<form method="post" class="form">
<label>Value 1 <input type="text" name="value1" placeholder="Enter 1st value"></label>
<label>Value 2<input type="text" name="value2" placeholder="Enter 2nd value"></label>
<h1>Select Operator </h1>
<input type="radio" name="operator" value="add">+
<br>
<input type="radio" name="operator" value="sub">-
<br>
<input type="radio" name="operator" value="mul">*
<br>
<input type="radio" name="operator" value="div">/
<br>
<input type="submit" class="btn btn-success" value="Show Result">
</form>
<?php
/*
Ensure that ALL post fields are set
*/
if( isset(
$_POST['value1'],
$_POST['value2'],
$_POST['operator']
)) {
/*
Assign POST values as variables.
*/
$value_1=(int)$_POST['value1'];
$value_2=(int)$_POST['value2'];
$operator=$_POST['operator'];
/*
Perform the relevant calculation
and assign the correct symbol for display
*/
switch( $operator ){
case 'add':$symbol='+';$res=$value_1 + $value_2;break;
case 'sub':$symbol='-';$res=$value_1 - $value_2;break;
case 'mul':$symbol='*';$res=$value_1 * $value_2;break;
case 'div':$symbol='/';$res=$value_1 / $value_2;break;
}
/*
print the sum and result
*/
printf(
'%d %s %s=%d',
$value_1,
$symbol,
$value_2,
$res
);
}
?>
</body>
</html>
I want to add another input or button next to the submit which will clear the form.
The problem I've found is that because the form uses $_POST to keep its values after the form is submitted (a feature I want to keep), it seems to prevent something like <input type="reset" value="Reset" name="reset" /> from working.
This is the code:
<form method="post" action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span><br>
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Reset" name="reset" />
</form>
There are a few things you can do here to get yourself going in the right direction. If you are using Javascript on the front end (and considering it's 2017 I think it's a safe assumption you are) then you could simply use whatever library you're using (read: Probably jQuery) and set the values to the empty string or un-check them.
Javascript / jQuery
I am very careful to only include inputs that are NOT the submit or reset button, otherwise you'll end up with two buttons on the bottom with no text.
Also note that $variable is my jQuery syntax for denoting to myself that this variable is a jQuery wrapped object.
$(function() {
$('[type="reset"]').on('click', function(e) {
e.preventDefault(); //We don't want the reset function to fire normally.
var $this = $(this),
$form = $this.parent('form'),
$input = $form.find(':input:not(:submit):not(:reset)');
$input.each((i, item) => {
var $item = $(item);
if ($item.is(':checkbox')||$item.is(':radio')) {
$item.prop('checked', false);
} else {
$item.val('');
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="email" name="email" value="jbob#app.com" /><br />
<input type="checkbox" name="something" checked />
<input type="radio" name="something_else" checked/>
<input type="submit" />
<input type="reset" />
</form>
PHP
For PHP what you want to do is actually add in another submit button, but change it's value. Like so:
<form method="post" action="<?php echo htmlspecialchars ($_SERVER[" PHP_SELF "]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span><br> E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span><br> Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span><br> Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br> Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female" ) echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male" ) echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br>
<input type="submit" name="submit" value="Submit">
<input type="submit" name="submit" value="Reset" />
</form>
Now, in your PHP code you do this at the top:
<?php if($_POST['submit']=='Reset') $_POST = array();
//you could also use [] to dictate an empty array in more recent versions of PHP.
///rest of code ///
Try Jquery form reset method.
put id attribute to form tag in your form.
<form id="Myform" method="post" action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br>
<input type="submit" name="submit" value="Submit">
<input id="Reset_btn" value="Reset" name="reset" />
</form>
I have set id attribute to reset button also to initialise reset function from there. Add jquery js above below code and test your reset button.
<script>
$('#Reset_btn').click(function(){
$('#Myform')[0].reset();
});
</script>
You can reset using JS.
<form is="myForm" method="post" action="<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br>
<input type="submit" name="submit" value="Submit">
<button onclick="document.getElementById("myForm").reset();" value="Clear form" />
</form>
Use jquery to clear the values.
add the button to reset the page and give it an id
<input type="button" id="reset" value="reset">
then do this
<script>
$("#reset").click(function(){
$("input").val("");
})
</script>
make sure you add the jquery library in the head
On click of reset button you can do something like this in php:
If(isset($_POST['reset'])){
$_POST = array();
}
I have form that has 2 radio buttons(Yes and No) and a text box. If the user clicks Yes it enables the text box you can input information and it is uploaded to the database including the value from the radio buttons. If you click no it disables the text box and suppose to upload the value of the radio box only to the database. But I am not getting that.
<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes" onclick="TermLeaseMonths.disabled=false">Yes |
How many months:<input type="hidden" name="TermLeaseMonths" value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">
I have a hidden input type that uploads the value. But when I click yes it does not disable the text box. Not sure where I am going wrong.
You forgot give id for the text field. Try this
<input type="radio" name="TermLease" value="No" onclick="TermLeaseMonths.disabled=true">No
<input type="radio" name="TermLease" value="Yes" onclick="TermLeaseMonths.disabled=false">Yes |
How many months:<input type="hidden" name="TermLeaseMonths" value="0" />
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" disabled="true">
Try this on click of radio button we can do by this way
$('#radio').click(function () {
if (("#radio").val() == "") {
$('class_name_oftextBox').attr("disabled", true);
} else {
$('class_name_oftextBox').removeAttr("disabled");
}
});
Or you can do this as
$('#enable').click(function () {
$('#textBox').removeAttr("disabled")
});
$('#disable').click(function () {
$('#textBox').attr("disabled", "disabled");
});
Demo jsFiddle
HTML
<form>
<span style="float: left;">
<label><input type="radio" name="TermLease" value="No" onclick="ShowHideTextbox('leaseMonths', false)" />No</label>
<label><input type="radio" name="TermLease" value="Yes" onclick="ShowHideTextbox('leaseMonths', true)"/>Yes</label>
</span>
<span id="leaseMonths" style="display: none; float: left;">
<span> | </span> <span>How many months: </span>
<input type="text" name="TermLeaseMonths" id="TermLeaseMonths" size="1" />
</span>
</form>
JS
var previousNumberOfMonths = "0";
function ShowHideTextbox(elementId, show) {
var link = document.getElementById(elementId);
var textbox = document.getElementById("TermLeaseMonths");
if (show){
textbox.value = previousNumberOfMonths;
link.style.display = 'block';
}
else {
link.style.display = 'none';
previousNumberOfMonths = textbox.value;
document.getElementById("TermLeaseMonths").value = "0";
}
}
For some reason my javascript won't execute when my submit button is pressed. It's supposed to leave an error message beside two textfields for a user's name and email address if they are empty, but it's not and I can't figure out why. Normally, when the two boxes are filled, the submit would go to a php page which sends an email. Any suggestions or help with fixing my javascript problem will be greatly appreciated.
This is my javascript:
//my javascript function
<script type='text/javascript'>
function validate_form()
{
$('span.error_message').html('');
var success = true;
$("#validate_form input").each(function()
{
if($(this).val()=="")
{
$(this).next().html("* You must complete this field"); // the error message
success = false;
}
});
return success;
}
</script>
and my form in html:
<form action=".....whatever....." method="POST" id="validate_form" onsubmit="return validate_form();">
<ol>
<li>
<span id="question">___ is your name? My name is Marie.</span>
<input type="text" name="q1" id="q1" />
</li>
<li>
<span id="question">___ are you from? I'm from Paris, France.</span>
<input type="text" name="q2" id="q2" />
</li>
<li>
<span id="question">Dave: ___ you like football</span>
<p>
<input type="radio" name="q6" value="1" id="q6_1" />
<label for="q6_1">Are</label>
<input type="radio" name="q6" value="2" id="q6_2" />
<label for="q6_2">Do</label>
<input type="radio" name="q6" value="3" id="q6_3" />
<label for="q6_3">Does</label>
<input type="radio" name="q6" value="4" id="q6_4" />
<label for="q6_4">Is</label>
</p>
</li>
<div id="username">
<p>
Before we begin, please enter your name and email:
<br />
<label for="user_name">Name: </label><input type="text" name="user_name" id="user_name" />
<span class="error_message" style="color:#FF0000"></span>
<br />
<label for="user_email">Email: </label><input type="text" name="user_email" id="user_email" />
<span class="error_message" style="color:#FF0000"></span>
</p>
</div>
<p style="width: 242px; margin: auto;">
<input type="submit" name="submit" value="Submit your answers" id="but" />
</p>
</form>
Edit: Fixed the problem with the javascript being called, but now the submit button isn't working even with the two textfields being filled.
$("#validate_form input") - I don't see validate_form anywhere; you need to add it or just use: $("input")
Firebug for firefox would help you debug these types of jscript issues. You can also use the Error Console in Firefox if you dont want to install firebug.
I'm having some trouble with some PHP.
Here's a shortened version of the HTML:
<label for="yes_or_no">would you like to tell me your favourite colours?</label>
<input type="radio" name="yes_or_no" id="yes" value="yes" />
<label for="yes">yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" />
<label for="no">no</label>
<div id="colours_box">
<label for="colours">great! please select from the following list:</label>
<input type="checkbox" name="colours[]" id="blue" value="blue" />
<label for="blue">blue</label>
<input type="checkbox" name="colours[]" id="yellow" value="yellow" />
<label for="yellow">yellow</label>
<input type="checkbox" name="colours[]" id="red" value="red" />
<label for="red">red</label>
<input type="checkbox" name="colours[]" id="green" value="green" />
<label for="green">green</label>
<input type="checkbox" name="colours[]" id="other_colour" value="other_colour" />
<label for="other_colour">other_colour</label>
<div id="other_colour_box">
<textarea name="other_colour_detail" id="other_colour_detail"></textarea>
</div>
</div>
The colours_box DIV is hidden and appears when #no is selected and disappears when #yes is selected using some basic JavaScript. The other_colour_box DIV does a similar thing- it's hidden by default and when #other_colour is checked it appears, when it's unchecked it disappears.
What I'd like it to do is this:
if 'yes' is selected in the first instance, all the checkboxes and textarea are ignored, even if they selected 'no' first and entered details to the checkboxes and textarea.
if the other_colour_detail textarea has been written in but 'other_colour' has subsequently been unchecked, nothing is returned for the 'other_colour_detail' textarea
Here's the PHP:
$yes_or_no = $_POST['yes_or_no'] ;
$colours = $_POST['colours'] ;
$other_colour_detail = $_POST['other_colour_detail'] ;
$colours_to_email .= implode("\n\t", $colours) ;
if (($yes_or_no == 'no') && ($colours != "")) {
$colours_to_email ;
}
if (($yes_or_no == 'no') && ($other_colour != "") && ($other_colour_detail != "")) {
$details_of_other_colour = ":\n\t$other_colour_detail" ;
}
This would then feed back to me via email something like this:
"Did they want to tell me which colours they preferred?\n" .
"$yes_or_no-\t" . "$colours_to_email" .
"$details_of_other_colour" ;
Thanks for having a look,
Martin.
You should disable the colours[] elements when "No" is checked. Disabled elements are not submitted:
<script type="text/javascript">
function toggle_colour_inputs(enabled) {
if ( "yes" ) {
document.form1.colours.disabled=false;
}
else {
document.form1.colours.disabled=true;
}
}
</script>
<input type="radio" name="yes_or_no" id="yes" value="yes" onchange="toggle_colour_inputs(this.value)" />
<label for="yes">yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" onchange="toggle_colour_inputs(this.value)" />
<label for="no">no</label>
<?php
$yes_or_no = $_POST['yes_or_no'] ;
$colours = $_POST['colours'] ;
$other_colour_detail = $_POST['other_colour_detail'] ;
$colours_to_email .= implode("\n\t", $colours) ;
if ($yes_or_no == 'no') {
if (count($colours) > 0) {
// colours to email
}
} else {
if (($other_colour != '') && ($other_colour_detail != '')) {
// details of other colour
}
}
?>