So I made a registration form such that the user must select one of the three radio options before continuing. However if the user didn't choose an option and clicked submit I want to display at least a message that says "please choose an option" or something. Here is my code so far:
<?php
$reg_type = "";
function setclick()
{
if(isset($_POST["Submit"]))
$clicked=1;
else
$clicked=0;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Reg_type"])) {
$clicked=1;
header('Location: Registration_1.php');
}
else {
$reg_type = $_POST["Reg_type"];
header('Location: Registration_2.php');
}
}
echo $clicked;
?>
<form name="frmtype" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
<?php
setclick();
if($clicked)
echo "Please select an option";
?>
I'm having a real hard time getting the logic down to display the error message if they didn't choose an option and clicked submit.
Your setclick() function will always set $clicked to true if the form has been posted. Since you then call setclick() after your main php block, it will be true whether or not the rest of the logic has changed it. Try this:
if(isset($_POST['Reg_type']) && $_POST['Reg_type'] != ''){
$reg_type = $_POST['Reg_type'];
header('Location: Registration_2.php');
} else $clicked = true;
and then down the bottom of the page under the form:
if($clicked) echo "Please select an option";
<?php
$reg_type = $_POST['Reg_type'];
if($reg_type == "1"){
//action 1
} else if($reg_type == "2"){
//action 2
} else if($reg_type == "3") {
//action 3
} else {
// no radio button is checked. display the message
$message = "Please select an option";
}
?>
Try this
<script>
function checkform(){
var checked = false;
var buttons = document.getElementsByTagName('input')
for (var i = 0; i < buttons.length ; i++) {
if (buttons[i].checked) {
checked = true;
break;
}
}
return checked;
}
</script>
<form onsubmit="return checkform()">
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
Check the fiddle
This should get you started. Please be aware that this script is selecting elements by tag name, which means it will pick up any input in the form. If you are OK with using jQuery I'd highly recommend it, it will make this kind of tasks trivial.
Related
I want to display the message when user clicks on Others checkbox. There are other checkboxes too so I want to compare checkbox value and if Others checkbox is selected then I want to display the message. This is my code:
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
php part:
if(isset($_POST['submit']))
{
if(!empty($_POST['subject']) && $_POST['subject']=="Others") {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
This code is not working. What changes should I make??
As subject[] is an array in
<input type="checkbox" name="subject[]" value="Others">
is an array type. So you should validate subject field as array as follows:
if (isset($_POST['submit'])) {
if (!empty($_POST['subject']) && in_array("Others", $_POST['subject'])) {
echo " <SCRIPT LANGUAGE='Javascript'>
window.alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');
</SCRIPT>";
}
}
You can try something like given below. you are free to use in_array() but if you want to use the second condition with it use foreach like this.
if (in_array("Others", $_POST['subject'])) {
echo 'selected ';
}
else{
//do something
}
OR
if(isset($_POST['submit']))
{
$results=$_POST['subject'];
foreach ($results as $result) {
if ($result==='Others') {
echo 'selected ';
}
else{
//do something
}
}
}
<?php
if(isset($_POST['submit']) && $_POST['submit']=="Submit"){
if(isset($_POST['subject'])){
$subject = $_POST['subject'];
if(in_array('Others', $subject)){
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}else{
echo "<script>alert('You wish to select other combo apart from given in above list. Kindly contact incharge of this institute.');</script>";
}
}
?>
<form method="POST" action="">
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2">Subject-1+Subject-2
<input type="checkbox" name="subject[]" value="Subject-1+Subject-2+Subject-3">Subject-1+Subject-2+Subject-3
<input type="checkbox" name="subject[]" value="Others">Others
<input type="submit" value="Submit" name="submit" class="wpcf7-submit"><br><br>
</form>
I have a 3 page registration site. When the user selects one of three options on the first page they can submit and move onto the 2nd page. There they fill out information and click submit which should take them to the third page. The problem is the jquery redirect code after clicking submit on the 1st and 2nd page is NOT working. This is the code in the first page:
<?php
session_start();
$errors = false;
$message="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!isset($_POST['Reg_type']) || !preg_match('/^[123]$/', $_POST['Reg_type'])) {
$message = "Please select an option";
$errors = true;
}
if (!$errors) {
$_SESSION["regtype"]=$_POST["Reg_type"];
header("Location: Registration_2.php");
exit;
}
}
?>
<div id="form_page1">
<form id="frmtype1" name="frmtype1" method="post">
<input type="radio" name="Reg_type" value="1"/> option1 <br/>
<input type="radio" name="Reg_type" value="2"/> option2 <br/>
<input type="radio" name="Reg_type" value="3"/> option3 <br/>
<input type="submit" name="Submit" value="Submit" />
<?php
if($errors)
echo $message;
?>
I'm trying to convert that php code into javascript and this is what I have:
<script>
$(document).ready(function(e) {
$("form").submit(function() {
var data= $(this).serialize();
alert(data);
$.post("/Registration_1.php",$("#form_page1").serialize());
window.location.replace("http://www.google.com"); //just for testing purposes
});
});
</script>
But for some reason the jquery stuff just WON'T work, so i have to use the php instead. Can anyone tell me what the problem is? Does the first argument of the post method have to be the page you want to go to (Registration_2.php) or the page you are asking data from? Hopefully if the first page's jquery code is fixed it can fix the 2nd page. I've been working/researching this problem for the past 3 hours to no avail. Please help, thank you.
Why choose such a complicated solution?
Form 1:
<form action="form2.php" method="post">
<input type="submit" >
</form>
Form 2:
<form action="form3.php" method="post">
<input type="submit">
</form>
etc. etc.
I am not very good at programming, I try to make the following radio buttons:
<input type="radio" name="yesno1" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check1">*please select</div>
<input type="radio" name="yesno2" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno2" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check2">*please select</div>
<input type="radio" name="yesno3" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno3" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check3">*please select</div>
<input type="radio" name="yesno5" value="yes" style="outline:0;"/>Yes
<input type="radio" name="yesno5" value="no" style="margin-left:30px;outline:0;"/>No</div>
<div id="check5">*please select</div>
<input type="button" value="submit" id="submit">
here is my javascript:
$("#submit").click(function(){
if (!$("input[name='yesno1']):checked").val()) {
$("#check1").show();
}
if ($("input[name='yesno1']):checked").val()) {
$("#check1").hide();
}
if (!$("input[name='yesno2']):checked").val()) {
$("#check2").show();
}
if ($("input[name='yesno2']):checked").val()) {
$("#check2").hide();
}
if (!$("input[name='yesno3']):checked").val()) {
$("#check3").show();
}
if ($("input[name='yesno3']):checked").val()) {
$("#check3").hide();
}
if (!$("input[name='yesno5']):checked").val()) {
$("#check5").show();
}
if ($("input[name='yesno5']):checked").val()) {
$("#check5").hide();
}
});
I know I am doing a clumsy way, what I want is that, if only one radio button is checked, the other three error messages should show, if two is checked, the other two should show, if three check , the other one should show, if no one checked, all of them show. However only the last case works at the moment, I don't know where I am doing wrong, can any of you help me, thanks in advance.
In addition, I am going to pass the data to the php file, so is it possible if I do:
$yesno1 = $_POST['yesno1'];
$yesno2 = $_POST['yesno2'];
$yesno3 = $_POST['yesno3'];
$yesno5 = $_POST['yesno5'];
Thanks for the kind help:)
Try this using jQuery is method. Also you can use else block instead of checking for if every time.
$("#submit").click(function(){
if (!$("input[name='yesno1']").is(':checked')) {
$("#check1").show();
}
else{
$("#check1").hide();
}
if (!$("input[name='yesno2']").is(':checked')) {
$("#check2").show();
}
else{
$("#check2").hide();
}
if (!$("input[name='yesno3']").is(':checked')) {
$("#check3").show();
}
else{
$("#check3").hide();
}
if (!$("input[name='yesno5']").is(':checked')) {
$("#check5").show();
}
else{
$("#check5").hide();
}
});
I have a problem with a wordpress widget that I developed.
It 's just a form with an email field, a button and a checkbox.
When the user click on submit, I want to verify if the checkbox is ticked and IF YES submit the form...
My problem is that the form is submitted even tough the checkbox is not ticked. The page is reloaded.
Here is a short version of my code :
<?php if(isset($_POST['rules']) && $_POST['rules'] == 'Yes')
{
echo 'The checkbox was selected';
} else {
echo 'The checkbox wasn\'t selected';
} ?>
<form id="form-invite" method="post" action="">
<label>Your friend(s) email :</label>
<br/>
<small style="text-transform:uppercase;font-family:Arial;font-size:9px;color:#333;"<em>Multiple emails separated by comma.</em></small>
<input class="kolom" type="text" name="email" id="email"/>
<input class="button-primary classic-button" type="submit" name="send-them" value="Send Invitation"/>
<input type="checkbox" name="rules" value="Yes" /> <span id="rulesInfo">I read the Privacy Policy</span><br/>
<span id="emailInfo"></span>
</form>
Add a simple onsubmit handler to the form, e.g.:
<script>
document.getElementById("form-invite").onsubmit = function() {
return this.rules.checked;
}
</script>
Demo: http://jsfiddle.net/vqVEF/
Here is my code in php. I have a php creditcard confirmation page, with 2 button, edit details and submit. I have an init file which will perform tasks based on what cc_confirm is and what editval is, confirm and editing details respectively.
if($_POST['cc_confirm1']=='y' && $_POST['$editval']!='y' && !isset($editval))
{echo '<input name="submitbtn" type="submit" value="Edit Details" /><input name="editval" type="hidden" value="y" /><input name="cc_confirm" type="hidden" value="n" />';
}
if($_POST['cc_confirm1']=='y' && $_POST['$editval']!='y' && !isset($editval)){
echo '<input name="submitbtn1" type="submit" value="Submit Card" /><input name="card1" type="hidden" value="y" /><input name="cc_confirm" type="hidden" value="y" />';
Now the problem is, because I am using two hidden items, always the one at the bottom is being executed. For this code, if i press on edit details, the details are being submitted, the credit card is being runned and then edit page is shown after that, which does not serve the purpose.
If i interchange both button codes, then even for submit card, it is showing only edit page details without submitting card. I have tried to change the name of the buttons but no use.How can i avoid this problem? Appreciate any effort to solve.
Why don't you split up the conditions by assigning a value to them
$continue = 0;
if($_POST['cc_confirm1']=='y'){
$continue++;
}
if($_POST['$editval']!='y'){
$continue++;
}
else{
$continue = 0;
$reason = 'editval';
}
if(!isset($editval)) {
$continue++;
}
else{
$continue = 0;
$reason = 'noeditval';
}
if($continue > 0){
echo '<input name="submitbtn" type="submit" value="Edit Details" /><input name="editval" type="hidden" value="y" /><input name="cc_confirm" type="hidden" value="n" />';
}
else{
if($reason == 'editval'){
//Process
}
elseif($reason == 'noeditval'){
//Process
}
}