I am trying to create a registration website where the users chooses amongst three options on the first page and after selecting, can move onto the second page where it is displays different information depending on the option selected previously.
On Registration_1.php this is the code:
<?php
$clicked = $_POST["Next"];
if(isset($_POST['Reg_type']))
{
header('Location:Registration_2.php');
}
elseif(isset($_POST['Next']))
{
header('Location:Registration_1.php');
echo "Error! You must select an option!";
// display form again here
}
?>
<form name="frmtype" action="Registration_2.php" 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="Next" value="Submit"/>
</form>
How can I redirect the user back to this page if they simply click submit without choosing an option and possible display a error message, and send them to page 2 if they choose an option? Thank you
Edit: Added if block, but still moved to next page regardless of selecting an option or not. Is the header() not the correct method to move to another page?
if(isset($_POST['Reg_type']))
{
// execute some code
// go to page 2
}
else
{
echo "Error! You must select an option!";
// display form again here
}
Documentation: $_POST variable
Related
I am working on a restaurant search and review project. I have a search page that will show restaurant names and provides a link for the user to add a review for each restaurant result by using the hidden input rid.
In my review.php, I am using the value sent in for the hidden input rid to run another simple query and then display the name of the restaurant. Then I have several inputs so the user can input their name, their rating of the restaurant, and finally a comment.
<form action="comment.php" method="GET">
<INPUT TYPE="hidden" NAME="rid" VALUE="">
<?php
$db=mysqli_connect(//parameters);
$restaurant_id= $_GET['rid'];
if (!empty($_GET)){
$result = $db->query(//query);
$row = $restaurant_result->fetch_assoc()
echo $row["name"];
}
?>
<INPUT //other inputs for the user>
</form>
My problem is that when the user hits a submit button at the bottom, the page will refresh with the url www..../review.php?rid= meaning that the name of the restaurant can't be displayed since no restaurant id parameter is being resent in the form. How can I send the restaurant id back to the page again once it is done?
I tried the top answer in this thread but I get the same url problem: Pass parameter to form action in HTML
<INPUT TYPE="hidden" NAME="rid" VALUE="<?php if(isset($_GET['rid'])){echo $_GET['rid'];} ?>">
I have a site in which an Admin User looks at rows of invoices which have been submitted by users, when they click on an 'Approve Invoice' button from one of these rows it will take them on the page below.
Once the Admin User approves this invoice, they hit the 'yes' radio button and submit at the bottom of the page which enters the value 'AUDITED' under the 'npc_active' column in that row. It then multiplies the quantity and points and inserts the total onto a new row in 'tally_points' (along with their user id and sales id). This is all working fine, but...
What I am trying to do, however, is make a condition in which once the sale is audited, that it can't be re-audited. ie the 'This invoice has been audited' print should show once the submission has taken place, but it isn't working.
I'm close but can't seem to figure out what the problem is. The code in which I think I am having the problem is below, the full page code is at the bottom of this post.
$str ='<form method="post" action="audit_invoice.php">
<font style="font-size:11px;">
<em>Is this invoice approved?<br />';
if($approved == "AUDITED") {
$str .='Please select carefully as this action cannot be undone.</em>
<em>Yes:</em><input type="radio" value="AUDITED" name="npc_active"> <em>No:</em>
<input type="radio" value=" " name="npc_active">
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</font>
</form></tr>';
}
else {
$str .='This invoice has been audited';
}
echo $str;
If I put the '==' before "AUDITED" it will show up with the echo 'The invoice has been audited' in each instance, if I put '=' in front of "AUDITED" it will show the yes button and submit button in each instance.
#AdamMC the = operator is only used when assigning data.
You are comparing a data, therefore you are correct when using ==
if($approved == "AUDITED")
I would like to request what exactly your $approve variable contains.
As of right now I can only make an assumption that this code implies
if invoice is approved it would equal audited which then would trigger it to echo "this invoice has been audited"
To stackoverflow users: please do not downvote, I cannot comment because my reputation does not permit it. Just trying to help
I've finally got the first page of my registration page working. The user has to select one of three options before continuing to the next page. The problem I am having now is that the first page is not sending the data to the next page. Here is the code for
Registration_1.php:
$reg_type = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!empty($_POST["Reg_type"])) {
//$reg_type=$_POST["Reg_type"];
//header('Location: Registration_2.php?rtype='.$reg_type);
$reg_type=$_POST["Reg_type"];
header('Location: Registration_2.php');
}
}
?>
<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
if(isset($_POST["Submit"]) && !isset($_POST["Reg_type"]))
echo "Please select an option";
?>
</form>
Registration_2.php
<?php
$regtype=$_POST["Reg_type"];
echo "regtype value is:" . $regtype;
if($regtype==1) {
?>
However regtype is blank, meaning i'm not getting any data from the previous page. Can anyone tell me what the problem is?
session_start();
$reg_type=$_POST["Reg_type"];
$_SESSION['cust_type'] = $reg_type;
and in any page,
session_start();
echo $_SESSION['cust_type'];
for more informations,
http://matthom.com/archive/2005/02/19/php-passing-variables-across-pages
http://www.plus2net.com/php_tutorial/variables.php
PHP Pass variable to next page
http://mrarrowhead.com/index.php?page=php_passing_variables.php
http://php.net/manual/en/reserved.variables.session.php
This is because you are doing a redirect, so the post data no longer exists.
You have a few options.
Instead of doing a redirect, you could do an include.
You could store the data (session, database, etc)
You could append the data to the redirect
header('Location: Registration_2.php?Reg_type=' . $_POST['Reg_type');
then use $_GET on the Registration_2 instead of post.
You're posting your form to page1 and then redirecting to page2. Page2 doesn't have access to the posted data due to the redirect (the post is not carried along).
What you should do is process the data in page1 and store it before redirecting (eg, in a session, or use a query string like you've commented out).
Another note, when you call for a redirect using header, make sure you also exit or die immediately afterwards like the php documentation mentions (since you cannot guarantee the page will stop processing there).
First of all, when you use header to redirect, the POST variables are lost. You need to pass the variables with GET in order to retrieve them on Registration_2.php.
Registration_1.php
//...
header('Location: Registration_2.php?Reg_type=' . $_POST["Reg_type"]);
//...
and Registration_2.php:
$regtype=$_GET["Reg_type"];
echo "regtype value is:" . $regtype; if($regtype==1) {
I have a form like this :
$form = '<form action="https://www.zarinpal.com/users/pay_invoice/'.$res.'"
method="post" target="_parent" ><input type="submit" value="Buy"/></form>';
$form .= '</form>';
echo $form;
I want to add a function to this form, so when "Buy" is clicked, then in MySQL a database is created for this user with the information of the selected item and his account id with a pending transaction status, and then the form redirects him to the webpage included.
I can manage the MySQL part, but the form only redirects the user to the given webpage, and i can not add users information to the database.
Is there a way I can add a database row for this user, then redirect him to the payment webpage ? ( except creating another page )
<form action="https://www.zarinpal.com/users/pay_invoice/<?=$res ?> method="post" target="_parent" >
<input type="test" name="customer-name" />
<input type="submit" value="Buy"/>
</form>
Then on the target page put:
<?
print_r($_POST);
## That will let you know what is being posted from the form
?>
To access the 'customer-name' variable for example just do:
$customerName = $_POST["customer-name"];
I'll try to explain this as best as I can.
I have a form that accepts multiple fields, and in the end, e-mails all the fields to a specific e-mail address.
So for example, I have three text boxes, one list box and two submit buttons.
Two of the text boxes are first name, and e-mail address
The third text box is used to populate the list box. So if I enter, NIKE, into the third text box and push the first submit button. Nike will now be in the listbox.
I want to be able to populate the list box with as many entries as needed, then push the second submit button to send all information (first name, e-mail address and all items in list box).
The problem is, pushing the first submit button always triggers the e-mail sent, since I'm "POST"ing.
I have everything working right now. The third text box submits the new data to a table in mysql, and then retrieves all the data and puts it in the list box.
What's the best way to fix this scenario? Could I stop the Post variable from validating, until the second submit button is used?
Also, I'd like to avoid Javascript, thanks
Make sure the two submit buttons have names. I.E: <input type="submit" name="command" value="Add"> and <input type="submit" name="command" value="Send">. Then you can use PHP to determine which one was clicked:
if($_REQUEST['command'] == 'Add')
{
// Code to add the item to the list box here
}
elseif($_REQUEST['command'] == 'Send')
{
// Code to send the email here...
}
BONUS: For extra credit, make the commands variables so they can be easily changed, and map them to functions...
<?php
$commands = array(
'doSendEmail' => 'Send Email',
'doAddOption' => 'Add Option',
);
function doSendEmail()
{
// your email sending code here...
}
function doAddOption()
{
// your option adding code here...
}
function printForm()
{
global $commands;
?>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="name"><br>
<input type="text" name="add">
<input type="submit" name="command" value="<?= $commands['doAddOption'] ?>">
<select>
<?php /* some code here */ ?>
</select>
<input type="submit" name="command" value="<?= $commands['doSendEmail'] ?>">
<?php
}
if(isset($_REQUEST['command']))
{
$function = array_search($_REQUEST['command'],$commands);
if($function !== -1)
call_user_func($function);
}