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.
Related
Okay this is kind of a noob question but I have been searching and searching and can not find out how to do this. I can not get the values of my radio buttons useing php.
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['doyoubuy'])) {
echo "You have selected :".$_POST['doyoubuy'];
}
}
?>
</body>
</html>
Thank you!!!
You are checking if (isset($_POST['submit'])) {
But your submit button is not actually named...
So just name it:
<input type="submit" value="submit" name="submit">
Then your code for the radios should work fine.
The action should be the current page. In your case is ""
I suggest this HTML and PHP for your consideration:
<html>
<body>
<form method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST) && $_POST != NULL) {
if ( $_POST['doyoubuy'] == 'Yes' || $_POST['doyoubuy'] == 'No') {
$answer = $_POST['doyoubuy'];
echo "You selected :". $answer;
}
}
?>
</body>
</html>
What needs to be tested is whether the form was submitted. If so, provided that each field bears a name in the form, then a value of some kind will be associated with every field, which obviates the need to test if the submit button is set. Also, if a user submitted a form but neglected to fill in, for example, a text input, then that field's value would still be set; it would be set to an empty string.
It is inadvisable to trust a posted form value and consider it safe to use. In this case, I test to be sure that the answer is what I'm expecting, just in case someone might have spoofed the form and changed the answer to something other than what I intended.
By default, if one leaves out the ACTION attribute of the form, its value is the url of the page containing the form.
Say I have a simple form on a page called photo/delete.php. This form deletes an image specified by the user. All it is, is this:
<form action="?" method="post">
<input type="checkbox" name="confirmDelete" value="confirm" />
<input type="submit" value="delete" />
</form>
So, this form contains a confirmation check box that must be ticked to ensure the image is deleted. How can I dynamically choose what page to POST this form to, based on its contents?
For example, if the checkbox is not checked, yet the submit button is clicked, I'd like to stay on the same photo/delete.php page and display an error, since its possible they really do want to delete the image and simply forgot to tick the box.
But otherwise, if everything is successful and the checkbox is ticked, I'd like to POST it to another page, say home.php since it makes no sense to stay on the same page of a just-deleted image.
How can I implement this?
You may try something like this
HTML:
<form action="delete.php" method="post">
<input type="checkbox" name="confirmDelete" value="confirm" />
<input id="btn_delete" type="submit" value="delete" />
</form>
JS:
window.onload = function(){
document.getElementById('btn_delete').onclick = function(e){
var checkBox = document.getElementsByName('confirmDelete')[0];
if(!checkBox.checked) {
if(e && e.preventDefault) {
e.preventDefault();
}
else if(window.event && window.event.returnValue) {
window.eventReturnValue = false;
}
alert('Please check the checkbox!');
}
};
};
DEMO.
This is a simple one, but I just can't figure it out.
I have a simple form:
<form action='#' method="post">
<label for="question-1">Question One</label>
<input type="radio" name="question-1-answers" value="1" />
<input type="radio" name="question-1-answers" value="2" />
<label for="question-2">Question Two</label>
<input type="radio" name="question-2-answers" value="1" />
<input type="radio" name="question-2-answers" value="2" />
<input type="submit" value="Submit Quiz" class="submit"/>
</form>
Then I have simple php in results.php to calculate the results:
<?php
$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$result = 0;
$a =array( 0=> "$answer1", 1=> "$answer2");
$result = array_sum($a)/count(array_filter($a));
echo "Your score is: "; echo round($result, 1);
?>
This all works great, but I want the result to show up on the same page as the quiz when the user presses the submit button, without a page reload. I know I need to use jQuery, but everywhere I look has a different way of doing it, and nothing works.
Edit:
So I added the following:
$function() {
$.get('../results.php', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
}
and
<div class="result">The results are:</div>
and updated the submit button with <input type="submit" value="Submit Quiz" class="submit" onclick="function()"/>
I'm still not getting anything when I click the submit button, and when I check the console the only error I get is: Uncaught SyntaxError: Unexpected token { but I don't know where I have an extra {.
You'll want to use jQuery to load a PHP quiz result HTML page. You then want to inject it into the dom.
Include jQuery, and then this example should work, assuming quiz result page is at (php/quizResult.php) - you can set this up as you like
jQuery
$.get('php/quizResult.php', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
HTML
<div class="result"></div>
Put the jQuery in a function in JavaScript, and call it when you want to load the results.
Here is an example as Christian has hinted at:
$.post('/callcenter/admin/postContacts', data, function(returnedData) {
// do something here with the returnedData
console.log(returnedData);
});
Your PHP page would then return JSON.
Ref. Pass data from jQuery to PHP for an ajax post
I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}
I'm creating a registration page and I would like to give the user the opportunity to review their information and go back and edit it before clicking a confirm button which inserts it into the database.
Is there a way to include two submit buttons which point to different scripts or would I have to duplicate the entire form but use hidden fields instead?
Any advice appreciated.
Thanks.
You can use two submit buttons with different names:
<input type="submit" name="next" value="Next Step">
<input type="submit" name="prev" value="Previous Step">
And then check what submit button has been activated:
if (isset($_POST['next'])) {
// next step
} else if (isset($_POST['prev'])) {
// previous step
}
This works because only the activated submit button is successful:
If a form contains more than one submit button, only the activated submit button is successful.
As for every other HTML input element you can just give them a name and value pair so that it appears in the $_GET or $_POST. This way you can just do a conditional check depending on the button pressed. E.g.
<form action="foo.php" method="post">
<input type="text" name="input">
<input type="submit" name="action" value="Add">
<input type="submit" name="action" value="Edit">
</form>
with
$action = isset($_POST['action']) ? $_POST['action'] : null;
if ($action == 'Add') {
// Add button was pressed.
} else if ($action == 'Edit') {
// Edit button was pressed.
}
You can even abstract this more away by having actions in an array.
you can
like :
http://sureshk37.wordpress.com/2007/12/07/how-to-use-two-submit-button-in-one-html-form/
via php
<!-- userpolicy.html -->
<html>
<body>
<form action="process.php" method="POST">
Name <input type="text" name="username">
Password <input type="password" name="password">
<!-- User policy goes here -->
<!-- two submit button -->
<input type="submit" name="agree" value="Agree">
<input type="submit" name="disagree" value="Disagree">
</form>
</body>
</html>
/* Process.php */
<?php
if($_POST['agree'] == 'Agree') {
$username = $_POST['username'];
$password = $_POST['password'];
/* Database connection goes here */
}
else {
header("Location:http://user/home.html");
}
?>
or via javascript
I certainly wouldn't repeat the form, that would be a fairly self-evident DRY violation. Presumably you will need the same data checks every time the form is submitted, so you could perhaps just have the one action and only run through the "add to database" part when the user hits the "approve" button.