use php session to list multiple form input - php

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

Related

Dynamically change html content based on form input after page refresh

I am currently attempting to create a simple test web page which takes in a user's input of their name through a form and then after the form is submitted, the page is refreshed and the page now ONLY displays "Hello NAME".
Now, after going to this specific URL, you are no longer able to enter the name again and you only see the "Hello NAME" phrase. Also, the page with the form and the final phrase must be reachable by the same URL - they must be the same page.
Any help would be greatly appreciated!
So from what I understand you want to get a value from $_GET or $_POST.
After it goes to the other url you can set the NAME set by the user on a $_SESSION variable, you just need to start the session at the start of the page session_start() and then add the name into the variable $_SESSION['name'] = $_POST['name'] or $_SESSION['name'] = $_GET['name']; depends on what you want to do or use. And don't forget about htmlentities() and strip_tags() to "sanitize" the variable or to make it more secure to use.
After that you should print something like
echo "Hello ". $_SESSION['name']; or anything like that.
Here's a very basic example of how to do that using a session and post/redirect/get.
<?php
session_start(); // be sure to start the session first
// **don't output anything before this**
// if the name has been added to the session, echo the hello message and exit the script
// (the form won't be displayed)
if (isset($_SESSION['name'])) {
echo 'Hello ' . $_SESSION['name'];
exit;
}
// if the name has been posted, save it to the session and redirect to the same page
// (the form won't be displayed)
if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
header('Location: ' . $_SERVER['REQUEST_URI']);
exit;
}
// If neither of those things are true, go ahead and show the form.
?>
<form method="post">
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
Using PHP Sessions is one solution, and another one that you can use is using the GET Parameters.
<form action="page2.php" method="get">
<input type="text" name="user_name">
<button type="submit">Submit</button>
</form>
In the example above, what the form will do is submit the entered value (from the user_name input) to access it on page2.php file (this is the form action attribute).
In your page2.php file, you can access the value from the form by using
<?php
/** $_GET['user_name'] is the name of your input in the form */
echo "Hello " . $_GET['user_name'];
?>
NOTE: $_GET parameters is not recommended when accessing sensitive data, as this value is displayed and can be edited by the end-user in the URL.

Php not working on other pages of my site

Guys I have an external php file and its code is
session_start();
$_SESSION['test'] = $_POST['click'];
$query= "SELECT * FROM software where id='".$_SESSION['test']."' ";
$result=mysqli_query($con, $query);
$row=mysqli_fetch_array($result);`
on my index page I have
<form method="POST">
aaaa
<input name="click" type="submit" value="2">
</form>
<?php print_r($row);?>
and it works completely fine. But on another page of my site called exten.php I have
<?php print_r($row); ?>
which dosen't work if i direct user here onclick of link on second line of index
If your
<?php print_r($row);?>
is the only PHP code in your exten.php, it won't display anything because the $row variable that is set in your index page is not reachable.
If I get it well, you want to display the result of the click action on the button on a second page, named exten.php. If so, the action attribute on the form tag must be set.
<form action="exten.php" method="POST">
But if you want to display the value of what you have put in session (the variable test), the you have to start the session and then call your variable.
session_start();
echo $_SESSION['test'];
That is true for any pages.
Not sure why you put the value of a form input into session and then put this session variable in your query but in any case, you should sanitize your input values before using them in a SQL query. For example:
$test = filter_var($_SESSION['test'], FILTER_SANITIZE_STRING);

Form / Submit Button in PHP

This .php file outputs items from an XML file onscreen and gives the user the chance to click on the add to cart submit button which is displayed after each item. The idea is that the ID, Name etc are further used in a shopping cart system, but I am still in the starting stages of the shopping cart. (This is for an assignment)
But the problem is that this method does not work since when all the iterations are done, irrespective to which button is clicked, the computer will always refer to the values stored in the variables in the last iteration of the for loop.
I want to write some code in order to be able to have a UNIQUE button for each ID, and when this button is pressed, the corresponding information to that item is put through thi: action="add.php"
Any help would deeply be appreciated because I am stuck, and I have no idea
<?php
$doc = new DOMDocument();
$doc->load('menu.xml' );
$Wraps = $doc->getElementsByTagName ("Wraps");
foreach ($Wraps as $w) {
$wrapsid = $w->getElementsByTagName("id")->item(0)->nodeValue;
$wrapsname = $w->getElementsByTagName("Type")->item(0)->nodeValue;
$wrapsprice = $w->getElementsByTagName("Price")->item(0)->nodeValue;
echo "Wraps ID: $wrapsid <br> Wraps Name: $wrapsname <br> Wraps Price: $wrapsprice";
?>
<form action="add.php" method="post">
<td class='view_td' width='10%'><center>
<INPUT TYPE=HIDDEN NAME='ID' value='{$wrapsid}'/>
<input type='submit' value='Add to cart' /></center></td><br>
</form>
<?php
}
?>
you can't put form tags around a TD it has to go around the whole table, either use divs and style it to replicate a table layout using CSS or use GET as you are only passing the ID back to the server eg. use an anchor instead of your input
To identify the submitted form, you can use:
•A hidden input field.
•The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
more information

Passing variable to third page

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

HTML form insert in SQL DB and also pass posted vars to page

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>

Categories