Submit form, visit next page, set session variables - php

I am working on a configuration proces with 6 steps. In each step I use a form which I submit and save the input data in SESSION variables.
On each step I have a "next button" which needs to submit the form of the page and redirects to the next step.
This piece of code checks if the submit button "wz_submit1" isset, then save the variables.
if(isset($_POST['wz_submit1'])) :
// Save wz_width in session
$wz_width = $_POST['wz_width'];
$_SESSION['wz_width'] = $wz_width;
// Save wz_height in session
$wz_height = $_POST['wz_height'];
$_SESSION['wz_height'] = $wz_height;
endif;
This is the "next" button:
<a class="wz_next_button" href="http://www.mynextstepurl.test">Next step</a>
This is my form:
<form id="wz_form1" method="post">
<ul class="wz_input">
<li>
<label>A</label>
<input name="wz_width" id="wz_width" type="text" value="<?php if(isset($_SESSION['wz_width'])) : echo $_SESSION['wz_width']; endif; ?>" />
<span>mm</span>
</li>
<li>
<label>B</label>
<input name="wz_height" id="wz_height" type="text" value="<?php if(isset($_SESSION['wz_height'])) : echo $_SESSION['wz_height']; endif; ?>" />
<span>mm</span>
</li>
</ul><!--End wz_input-->
<input name="wz_submit1" type="hidden" />
</form>
If I use a normal submit button for "wz_submit1" it submits the form right and saves the variables in session. But after that there's no redirect to next page.
If I put a onclick form submit on the "next" link, the redirect to next page goes right but my variables doesn't get set.
I hope you understand my problem and hope you can help me.
If you have a better idea on how doing it (for example without a form for each page) then let me know.
Thanks!

you can do it this way... when form submits and all the variable gets saved then it will redirect to your next page...
if(isset($_POST['wz_submit1'])) :
// Save wz_width in session
$wz_width = $_POST['wz_width'];
$_SESSION['wz_width'] = $wz_width;
// Save wz_height in session
$wz_height = $_POST['wz_height'];
$_SESSION['wz_height'] = $wz_height;
header('Location : www.yournextpage.com');
endif;
your next button and from will be...
<form method="post" action="samefile.php">
//your other fields
<input type="submit" value="next" name="wz_submit1"/>
</form>

Related

How do i make form variable to display on all my site pages?

This is my form:
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
When i submit my form, the submitted form variable displays well on the "tracking.php" page using <?php echo $_POST['trckno_trk']; ?>.
But when i click on other pages in the site, it doesn't seem to display. That mean that the form variable echo $_POST['trckno_trk'] displays only on one page but does not display on any other page.
Please, how can i get it to display on every other page on my site.
Try this
<?php
// Start the session
session_start();
?>
Then you html
<form id="form1" name="form1" method="post" action="tracking.php">
<label>
<input type="text" name="trckno_trk" id="trckno_trk" />
</label>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</form>
Then you should save the variable in your Session
<?php echo $_POST['trckno_trk'];
$_SESSION["trckno_trk"] = $_POST['trckno_trk'];
?>
Now you can use this Session to display on other pages.
Form submited data are available only for page you specified in action parameter. They are not stored in any way. You need to save them for example in database or SESSION variables and retrive them again when accessing other pages.
Form parameters are accesible only when you post them, to use them later you will need to save them somewhere.
If you'd like to save the value & display it everywhere for all clients, you will need to store it in a database (e.g. MySQL), fetch the value and print it
If you'd like it to be unique for each client, you could use cookies, for example:
setcookie('cookiename',
'the value',
time()+86400 /* seconds until it expires */,
'/' /* On what pages do you want to use it, '/' means all pages*/,
);
The code above saves the cookie, to print its value:
echo $_COOKIE['cookiename'];
Another option is to save it in the session, but it will expire after a short time
session_start(); // put this on the start of every page to start the session
$_SESSION['name'] = 'value'; // save a value in the session
echo $_SESSION['name']; // print it
I suggest you use $_SESSION[] to store your POSTED value, this way you will be able to access it on any other page.
So on your tracking.php you can have code like this
session_start();
$_SESSION['trckno_trk']=$_POST['trckno_trk'];
then on other pages you can access the variable using
session_start();
echo $_SESSION['trckno_trk'];

Add data to sql on button click

my page receives data which i retrieve with $_post. I display some data and at the bottom of page my button has to save data to mysql. I could submit form to next page, but how do i access the data that I have retrieved with post then? Lets say i have following code (in reality alot more variables ..):
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
You can do it in two methods:
1) You can save the POST variable in a hidden field.
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
The hidden value also will get passed to the action page on FORM submission. In that page you can access this value using
echo $_POST['somevalue'];
2) Use SESSION
You can store the value in SESSION and can access in any other page.
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
and in next page access SESSION variable using,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
Take a look. Below every thing should be on single php page
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>

isset function is not taking variable passed from other page

here i am getting a value from previous page with form here i assign the value to php variable $foodid i want to echo its value after the continue button is clicked
//its value is passed from the previous page form with action to this page
$foodid = $_REQUEST['foodid'];
//as soon as continue button is clicked i want to display $foodid
<form method="post" action="">
<input type="submit" name="continue" value="continue">
</form>
if(isset($_POST['continue'])){
echo $foodid;//here the foodid variable must be declared
}
PHP is a server side languaue
JAVASCRIPT - is a client side language
After redirecting to new page , you have the value with your self, but displaying it on click is possible with javascript only (will display the number without refreshing the page)
in PHP - its not impossible, but it does not make sense to redirect to same page with some additional parameters's to display
eg; on click continue , submit a form with no action and there form should have that input field with value which you want to display (can be in hidden type), it will get submitted to same page and you will get your value using
$_REQUEST['field_name'];
But its not recommended , use JS for this, people purposely use JS for such kind of things
You should pass the $foodid value through form to the php page. This can be done by declaring a hidden variable and assigning foodid value to it.
Try this
<?php
$foodid = $_REQUEST['foodid'];
?>
<form method="post" action="">
<input type="hidden" value="<?php echo $foodid ?>"
<input type="submit" name="continue" value="continue">
</form>
<?php
if(isset($_POST['continue'])){
echo $foodid = $_POST['foodid'];
}

Ignoring initial loading of PHP_SELF

I am learning PHP and I have a page that reloads back to itself. I want to know if you can ignore a certain function on the initial loading of the page and only call it once the form submit button has been clicked.
The page is passed a 'ticketID' and loads the information from it. I then want to be able to add a note using the following form method:
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<strong>Add Note:</strong>
<textarea name="note" rows="5" cols="40" value=><?php echo htmlspecialchars($note);?></textarea>
<span class="error">*<?php echo $noteErr;?></span><br>
The user then clicks on a submit button to submit the note for processing:
<button type='submit' name='ticketID' value= <?php echo $_POST['ticketID'];?> >View</button>
</form>
The 'ticketID' is then passed back to the page to reload the information.
If the submit button is pressed and no note has been entered I want a message box to display informing the user to include a note. I have tried:
if (!empty($_POST["note"]))
{
echo "This has updated...";
} else {
echo "Missing!";
}
However this loads the error message even on the initial load of the page. I have tried setting a variable to the POST ticketID value and clearing the POST value after the page has displayed and before testing for the error message:
$tempTicketID = $_POST['ticketID'];
$_POST['ticketID'] = NULL;
Then testing the error message, and finally setting the POS value back before the page ends to allow it to reload correctly again:
$_POST['ticketID'] = $tempTicketID;
However the POST value doesn't save and the page reloads with no information.
Any help would be great appreciated.
Here's the full code layout:
##LOAD THE PAGE INFO...
#Set the temp variable and clear the post value
$tempTicketID = $_POST['ticketID'];
$_POST['ticketID'] = NULL;
#Load the form
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<strong>Add Note:</strong>
<textarea name="note" rows="5" cols="40" value=><?php echo htmlspecialchars($note);?></textarea>
<span class="error">*<?php echo $noteErr;?></span><br>
<button type='submit' name='ticketID' value= <?php echo $_POST['ticketID'];?> >View</button>
</form>
#Test if the note is empty and the form button has been pressed
if (!empty($_POST["note"]))
{
echo "This has updated...";
} elseif (empty($_POST["ticketID"] {
echo "Missing!";
}
#Set POST value back to reload the page
$_POST['ticketID'] = $tempTicketID;
We need to restructure the form just a bit to make this happen. You can check if the form is submitted by testing for the button that must be clicked to submit. However, you're using that button for multiple purposes. To simplify, we'll have a separate submit button, and pass the ticketID value through the form with a hidden input. You shouldn't need the code that unsets the $_POST values.
<button type='submit' name='submit'> View</button>
<input type='hidden' name='ticketID' value= <?php echo $_POST['ticketID'];?> />
Then you can test if the form has been submitted with this quick check:
if (isset($_POST['submit'])) {
if (!empty($_POST["note"]))
{
echo "This has updated...";
} else {
echo "Missing!";
}
}

How to add a function to a form

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"];

Categories