How to correctly navigate to same and different page - php

Below is my code for a simple form in the create_session.php page. I am using the forma action method to navigate the user to the "QandATable.php" page when the user submits the form. But what I want is that if the user types in the number 1 in the number of sessions textbox, then navigate to the QandATable.php page wheh the user submits the form, else if it is any other number, then when the user submits the form, I want it to go back to the "create_session.php" (Back to its own page). Imagine it like you click on the submit button and it refreshes the page so it goes back to being a blank form, thats what I want to do if the number of sessions textbox contains a number which is bigger than '1'. How can this be done? I am using php and jquery code as well as basic html.
Thank You
<form action="QandATable.php" method="post" id="sessionForm">
<p><strong>Number of Sessions you Require:</strong> <input type="text" id="sessionNo" name="sessionNum" onkeypress="return isNumberKey(event)" maxlength="5" /></p>
<p><input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion" onClick="myClickHandler(); return false;"/></p> <!-- Prepare Questions here-->
</form>

At the very top of the QandATable.php script add:
if ($_POST['sessionNum']!=1) {
header("Location: create_session.php");
exit();
}
But as others have mentioned in the comments, you might as well just make the form submit to the create_session.php page and if it's successful then redirect to the next page.

Related

How can a submit button made to be act as a link too?

I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);

load form using AJAX will loose form and values after a failed submission

I have a PHP page that loads several parts of a form using AJAX. For instance, first check if the user is already registered, if so the script loads (with AJAX) the rest of the form. The form will not be submited using AJAX what can be a problem when the user submits the form (without AJAX) - imagine there are some errors - the form will loose all values.
I'm wondering if CSS hiding part of the form and after the successful login use JS to display the rest of the form, would be better.
Here some code:
<form action="some_action.php">
Email: <input type="text" name="email" id="email"> <br />
Password: <input type="password" name="password" id="password"> <br />
<button id="vrf_login">Verificar</button>
<div id="rest_form">
</div>
</form>
AJAX:
- CHECK login: if email and password matches then
- LOAD the form for div with id "rest_form"
(it is in another file, for instance:
<input type="text" name="place" id="place">
<input type="text" name="age" id="age">
<input type="submit" name="submit" value="submit">
)
The problem is if I submit the form (without AJAX) and there are errors I will loose the form loaded with AJAX
EDIT (again)
Thank you all for your constructive suggestions:
The solution I adopted is close to the first Alkis's suggestion:
almost all the form is hidden (CSS)
after some logic choices the (part of the) form is turned visible (jQuery) - to "remember" what parts should be visible in case of submission failed (server side validation) some session variables hold the information (AJAX) - and then, after the submission (failed) use jQuery to restore the prior form structure (get the session variables with JS this way: var xpto = "<?php echo $_SESSION['prior_xpto']; ?>" ; )
the fields of the form will remember theirs values (with PHP)
You have 3 options.
Stop loading the whole form by ajax. Hide it with css and show it if the the conditions are met. If the page is shown after some validation error, just show it (change the css inline or give it a different class)
Have a condition and every time the page loads check if it is a first load or if the page is shown after some validation error occured. If the latter is true then load again the form with ajax. This condition can be a hidden field that takes its value from the server and you check it on the client every time you serve the page.
The second solution can be done on the server too. Have the condition be checked on the server. If it's a first load, then don't populate the form and let it be populated from ajax as you do now. If it's after a validation error then pre-populate the form. It's just an if/else clause.
Please provide some codes for your question, but i guess your problem is sending result using a button with "submit" type !
if you have a form like this:
<form>
<inputs ...>
<input type="submit" value="Send data" onclick="SendDataUsingAjax()" >
</form>
after clicking on submit all values on input will reset regardless of what your ajax function is doing. to fix this problem you only need to change type="submit" to type="button".

submit a form with a link from another page

I have created 3 php pages. The 1st includes the following form:
<form method="POST" name="go" action="search_form_all.php" >
<input name="value" type="text" id="search_form_1" size="65" placeholder="enter name"/>
<input type="submit" value="" name="submit" />
</form>
The 2nd page called "search_form_all.php" includes the php codes that displays the results of the above form. So if the user type a name in form and press submit, the "search_form_all.php" displays all names from my database according to what the user inserts in the search form.
All I want is to have in my 3rd php page a link, that when the user press it to execute the form in 1st page. For example if I enter a name in search form like "john", then I want to be able to go to my third page and press the link, the form to be executed and to return all names "john" from my database. Is this possible?
Yes, this is possible with session variables. An example:
<?php
session_start();
if(isset($_SESSION['views'])) {
$_SESSION['views']=$_SESSION['views'];
} else {
$_SESSION['views']=1;
}
echo "Views=". $_SESSION['views'];
?>
You can also refer to Document Link
Let me know if you need more help.

Form refresh after submit

The form below can contains different elements of text fields, drop down and selection boxes, which allows the user to update his profile. The process of updating MySQL fields is being done after form submits.
<form method="post">
My Name: <input name="myname" type="text" value="<?php echo $_SESSION['SESS_MY_NAME']; ?>" /><br />
My Email: <input name="email" type="text" value="<?php echo $_SESSION['SESS_EMAIL']; ?>" /><br />
<input type="submit" name="Submit" value="Update" />
<?php
if (isset($_POST['Submit'])) {
// MySQL update
;
;
;
$result=mysql_query($sql);
}
// if successfully updated, make form refresh.
if($result){
;
;
;
}
?>
</form>
I want to refresh only the form, so that the user will stay in the same page and will see the changes that he made (i.e. What do I need to put under the comment in the code “if successfully updated, make form refresh”?).
I cannot use header("location: samepage.php"); because I have too many HTML codes involved in between and before.
Appreciate any help,
Move the isset($_POST['submit']) check to the top.
Your current (intended) process is as follows:
Retrieve form data from DB/Session/etc.
Display as HTML
User submits form
Repeat steps 1/2
Write data to DB
Force refresh
Repeat 1/2 again
If you move this check to the top, the process is changed to:
Retrieve form data from DB
Display as HTML
User submits form
Write data to DB
Repeat 1/2 (it will now retrieve the updated information and display correctly).
This is the absolute simplest way you can do it. It doesn't take into account Post/Redirect/Get or updating the session so multiple DB reads are unncessary, etc.
Why don't you use jQuery AJAX to submit and verify the form then output the result in chosen div element.
More on that available at nettuts+
Such helpful. Much wow.
Thankfully I was able to solve this on my own after some intense googling since no one else seemed to have an answer. For anyone looking to accomplish the same thing, all you have to do is add the following code to the "Additional Settings" section of your CF7 form configuration:
on_sent_ok: "location = 'http://domain.com/contact';"
Just replace the URL with the URL of your form page and presto, it'll automatically refresh back to that page when the form is submitted!

Prevent or clear back history with form submission

I have a small quiz web application to deploy.
Basically I have 2 pages, '1) Quiz page' and '2) Result page'. Upon submitting the Quiz form, it will generate the result on the result page for the user. However I need to prevent user from clicking on back button or keying in Backspace on the keyboard.
I researched the use of 'location.replace()' but have no idea how can I implement it within my form:
<form id="quiz" name="quiz" method="post" action="result.php">
...
<input type="submit" name="Submit" value="Submit" />
</form>
I would like to know if there is any way for me to use location.replace() within the form, or otherwise, any other way that I could prevent/clear user from getting back to the history page.
Thank you.
If you're afraid of users going back checking answers then resubmitting the form, I think you're approaching the problem from the wrong end.
What you need to do is give each quiz "session" a unique ID at startup. Then do a check on form submit to see if the quiz session has already been submitted. If it has already been submitted, deny it.
There is no way to prevent a user from clicking "Back" in his/her browser. One way to accomplish the aim is to submit the form using AJAX. When he/she clicks back, it will not go to the quiz but to the previous page.
The following fully-working example uses the jQuery library:
<html><head></head><body>
<div id="form_wrap">
<form onsubmit="send_form(this);return false">
...
<input type="submit" />
</form>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
function send_form(form) {
jQuery.post('result.php', jQuery(form).serialize(), function(data) {
jQuery('#form_wrap').html(data);
});
}
</script>
</body></html>
just submit form in onload event.
<body onload="document.forms[0].submit()">
This will skip adding page to history.

Categories