MY Problem is that i have a link on a PAGE 1 with a variable associated with it
for ex
Spring
when user click this link i forward him to an html page Say PAGE 2 containing registration form when he fills form and click submit the details of form the data he inserted is grabbed by another php page Say PAGE 3 with request.
What i want is that the Variable associated with the link (Spring) on Page 1 may also be passed to this third page where the form data and the variable associated with link on page 1 may be processed
What you have to do is take the passed data from the link (Spring) and keep the in the hidden field as below. You will need php $_GET array for this and your page should have .php extension.
<input type="hidden" name="Spring" value="Spring value">
<input type="hidden" name="Spring2" value="Spring value 2">
Keep the hidden input fields within your registration form. When the registration form is submitted these hidden filed will be submitted along with the form. Then you can retrive them as you are now retrieving the registation values.
change your registration page extension to .php and then inside form include an hidden field like below
<input type="hidden" name="family" id="family" value="<?php echo $_GET["family"];?>">
Take one hiddne field in registration form such as
<input type="hidden" name="passed_var" value="<?php echo $_GET['family']; ?>" />
You may wish to look into Sessions in order to this
Sessions are Superglobals that can be activated by using the PHP
session_start()
command.
You can then store and access the SESSION variables like this:
PAGE1.PHP
<?php
session_start();
$_SESSION['foo'] = "bar";
?>
PAGE2.PHP
<?php
session_start()
$foobar = $_SESSION['foo'];
echo $foobar;
//echo's: "bar";
?>
You can read more about Sessions here and here
Related
So, I was working on a PHP Application Center (Form), and I'm stuck at something.
I have 3 pages of form and 1 sumbit page. All are in .php extention. So, I want to take the <input name="name" type="text" placeholder="Name" required> from page one and:
<label>About yourself (Atleast 50 Words): <span>*</span></label><br />
<textarea name="yourself" placeholder="About yourself (Atleast 50 Words)" ></textarea> to the submit page.
I can take the values of Textarea & Textbox from page 3 to submit page through <?php echo $_POST['somethinghere']?>. But I can't take values from the First and second page.
Here are links to my php codes with style.css (pastebin.com):- http://pastebin.com/81vgHh5H
I had a look at the Pastebin and I think I can see the problem. You're setting the Session data on the same page that they're filling out (so you're assigning those fields to the session before they've even been filled in). You need to do that on page 2, not page 1. So your pattern is thus:
Show page 1 form.
User fills it out and hits submit.
Send user to page 2.
Page 2 grabs the information posted from Page 1, stores it in the session.
Page 2 shows the second form page.
User fills out page 2 and hits submit.
Send user to page 3.
Page 3 grabs the information posted from Page 2, stores it in the session.
And so on...
By the time you get to the last page, your session will contain all the information from the form and you can then process it however you need to.
Example:
Page 1:
<form action="page2.php">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
$_SESSION['page1text'] = $_POST['page1text'];
?>
<form action="page3.php">
<input type="text" name="page2text"/>
<input type="submit"/>
</form>
Page 3:
<?php
$_SESSION['page2text'] = $_POST['page2text'];
?>
<form action="page4.php">
<input type="text" name="page3text"/>
<input type="submit"/>
</form>
Page 4:
<?php
$_SESSION['page3text'] = $_POST['page3text'];
?>
<ul>
<li>Page 1: <?php echo $_SESSION['page1text'] ?></li>
<li>Page 2: <?php echo $_SESSION['page2text'] ?></li>
<li>Page 3: <?php echo $_SESSION['page3text'] ?></li>
</ul>
As you can see, page 4 winds up with access to all of the information from the previous pages.
Edit: This answer previously read: I would recommend having a model that contains all of the form's properties. As they fill out each page, populate the model with the data the user has entered and then when they successfully complete the form you can destroy the session data.
You can store them in session like, pass your data from page one to page two and in page two do following
$_SESSION['name'] = $_POST['name'];
Again, pass your data from page two to page three and in page three do following
$_SESSION['yourself'] = $_POST['yourself'];
This will set your name and yourself data in session and will be accessible from any page. Do not forget to include session_start() at the top of your page if that page uses session.
Now, you can access those session variable from submit page like,
echo $_SESSION['name'];
echo $_SESSION['yourself'];
I am new to php. How can I use session to remember my multiple form input like this:
In the first webpage, I can input for example a product name, when I click submit, the second page can tell me the product name I typed in. AND, if I go back to the first page again, and input another product name, and click submit, the second page can display the first product and the new product. If I input more product names continuously in this way, I can see all products listed in the second page.
How can I use session to do this?
First page (order.html):
<html><body>
<h4>Order Form</h4>
<form action="showorder.php" method="post">
Product name: <input name="prodname" type="text" />
<input type="submit" />
</form>
</body></html>
Second page (showorder.php):
<html><body>
<?php
$Pname = $_POST['prodname'];
echo "You ordered ". $Pname . ".<br />";
?>
</body></html>
You can use the $_SESSION array in the following fashion:
$_SESSION['products'][] = $_POST['prodname'];
That means that $_SESSION['products'] will be an array containing all the purchased products.
Don't forget to call session_start() in the beginning of all relevant pages, and filter your code against SQL injections and such.
session_start();
$_SESSION['everthing'] = $_POST['username'];
Then you can echo out the session, or just assign a variable to it. Sessions will stay there until the browser is closed
I have come across a situation I am not sure how to hndle. I am new to this, but I do understand the server side realm of php vs user side of the browser.. I just dont know how to accomplish what I want to do..
I have a form on a page where a user can enroll in a school course. They select the course type, location, date, and payment type.
On submit it goes to an outsourced shopping cart for payment, which uses PHP vars to populate the item description, price, ect.. along with our store id and other pertinent information.
I ALSO need to insert some of the PHP vars into the user database.
I tried having the form action go to another PHP page which process the DB entry then redirects to the cart, but when I get to the cart an error is generated saying the info was not submitted properly.. but the DB entry was successful.
I also tried using an include(dbentry.php) in the form action with the cart link.. this generates a server side error on loading the page.
At one point I successfully had it create a db entry (although it was blank) AND successfully redirect to the cart with all of the vars there, but a blank DB entry does me no good. I am assuming entry happened before the $POST vars were created... I also have changed so much I dont remember how I did it and cannot reproduce that..
My main question is:
How can I have a user fill out an HTML form, and when submitted perform the DB entry with the $POST vars while also directly passing the $POST vars to the cart page? Normally I would run the dbentry.php on the next page, but I have no access to scripting on the outsourced cart page...
You can try to use hidden textboxes to hold the values of the form! And this value can be accessed from different php pages
You will need to pass your variables from page1.php to page2.php to outsourced cart. I would do something like the following:
What the below code is doing:
Send original form data using POST to page2.php
Page2.php will then read the POST data (you can now do what you want with this data, whether it be store it into a database, etc.). A Javascript snippet will then submit the form to your checkout cart page (page3.php) with the necessary POST variables which are being stored as hidden fields within the form.
Page1.php
<form action="page2.php">
<input type="text" name="myfield1"/>
<input type="text" name="myfield2"/>
<input type="text" name="myfield3"/>
<input type="hidden" name="myfield4"/>
<input type="hidden" name="myfield5"/>
<input type="hidden" name="myfield6"/>
<input type="hidden" name="myfield7"/>
<input type="hidden" name="myfield8"/>
</form>
page2.php
<?php
if(isset($_POST['myfield1']))
{
$myfield1 = $_POST['myfield1'];
}
// do the above for all of the fields, use the data to query database with.
// Perform database operations here
?>
<form action='Page3.php' method='post' name='frm'>
<?php
foreach ($_POST as $a => $b) {
echo "<input type='hidden' name='".$a."' value='".$b."'>";
}
?>
</form>
<!-- Script to submit button -->
<script language="JavaScript">
document.frm.submit();
</script>
I have setup a form that allows a user to make selections from drop-down lists, and clicking the submit button will redirect them to the appropriate page based off of their selections. Here is the PHP:
<?PHP
function redirect($where){
header("Location: $where");
}
if ($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/73-prodemo-xp- firefox');
}elseif($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/72-prodemo-xp-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/74-prodemo-win7-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/75-prodemo-win7- firefox');
}
?>
I am manually telling the query where to take the user.
My dilemma is that I want a simple line that says:
"Hi, _. How can I help you?"
The blank would be filled in by the name that is inputted on the form as the Caller's name.
Here is an example of the form:
<form action="" method="post" name="form1">
Caller's Name: <input type="text" name="callersname" /><br />
<select name="os1"> <option selected="selected" value="xp">XP</option>
<option value="win7">Win7</option> </select> <br />
<select name="browser"> <option selected="selected" value="ie8">IE8</option>
<option value="ffx">Firefox</option></select> <br />
<input type="submit" value="Next" />
I'm very new to PHP. If I create a PHP page to post the caller's name, then how do I avoid the page redirecting to that php page instead of the redirect I have manually setup with the drop-down menu options selected?
Thanks for any advixce
If you want to store the user's name on a page other than the first page or the same page that is processing the form values (IE the script that's getting the redirect) you want to store that information either in a database, or more simply in a session.
First, initialize the session on each page where you need to pull that information from:
session_start();
Then, create a session variable (unique to every user/browser) like so:
$_SESSION['user_name'] = $_REQUEST['callersname'];
Now, on the page you wish to retrieve their name (IE Hi ) just do so like this:
echo "Hi {$_SESSION['callersname']} welcome to mcdonalds how may I help you?";
In HTML a form will submit to the url that is given in the value of the action attribute of the form tag. In your example above you have:
action=""
This tells the browser to submit the form to the current page.
It sounds like you are trying to do two somewhat unrelated things in the same step. Are you trying to get the caller's name or have them choose what url to go to? If you want to do both, you may be better off by just accepting the os1 and browser values as POST variables, and dynamically changing the page based on the value of those variables rather than redirecting the user to entirely separate pages.
Try using $_POST instead of $_REQUEST... It may be the problem and too, add action="yourpage.php" to tell the code to send the form to the current page or another page...
I have a some hidden values in one page. I want to create a session and navigate with the hidden fields to the next page or another page and access the hidden values.
How can I do that? Please give me an example.
Try:
<form action="blah.php">
<input type="hidden" name="the_value" value="content" />
Then do the following in php:
<?
session_start(); // Start the session
$_SESSION['the_value'] = $_POST['the_value'];
?>
Now you have the value stored in the session. Be careful if you want to save this to MySQL. You need to sanitize the string first!