I'm trying to pass a dynamic value, an ID of a product. Through a form so I can write in a mysql database.
My problem is that the value is not being passed on through the form. I suspect it's due to that I'm trying to pass a value from one table to another table. Should I set a temporary variable for this $item_id instead? Or is it possible to pass anyway? Please help..
My shopping cart, here I echoes out the $item_id, which shows the last $item_id visible on the browser:
<div>
<?php
echo $item_id
?>
<form method="post" action="checkoutpage.php">
<input type="hidden" name="product_bought" value="<?php echo $item_id ?>">
<input type="submit" value="Submit">
</form>
checkoutpage.php:
<?php
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>checkout form</title>
</head>
<body>
<form action="insert.php" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email"><br />
Credit Card: <input type="text" name="credit_card">
<input type="hidden" name="product_bought" value='product_bought'>
<input type="submit">
</form>
</body>
</html>
insert.php
<?php
$con=mysqli_connect("localhost","xxxx","xxxx","mystore");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO orders (name, email, credit_card, product_bought)
VALUES
('$_POST[name]','$_POST[email]','$_POST[credit_card]','$_POST[product_bought]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
on checkout page you have:
<input type="hidden" name="product_bought" value='product_bought'>
and you should have:
<input type="hidden" name="product_bought" value='<?php echo $_POST['product_bought']; ?>'>
You are passing "product_bought" as the value.
I hope it fixes it.
Related
Hello I have problem with my php code it won't insert value to the database and when it does the value is duplicate.
Here is the php code:
if (isset($_GET['addform']))
{
include '../includes/db.inc.php';
try
{
$sql = 'INSERT INTO author SET Author_name = :Author_name, Author_email =:Author_email';
$s = $pdo->prepare($sql);
$s->bindvalue(':Author_name', $_POST['Author_name']);
$s->bindvalue(':Author_email', $_POST['Author_email']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted author.';
include 'error.html.php';
exit();
}
header ('Location COMP1321/recipes/admin/authors/authors.html.php');
exit();
}
And here is the html form
<? php include 'index.php' ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php html($pageTitle); ?></title>
</head>
<body>
<h1><?php html($pageTitle); ?></h1>
<form action="?addform" method="GET">
<label for="name"> Name: <input type="text" name="Author_name" id="Author_name"></label>
<br/>
<label for="email"> Email: <input type="text" name="Author_email" id="Author_email" ></label>
<br/>
<input type="hidden" name="id" value="<?php html($id); ?>">
<input type="submit" value="<?php html($button); ?>">
</form>
</body>
</html>
Any idea what went wrong here?
In your HTML code:
<form action="?addform" method="GET">
You are using GET as form method
And in your PHP code:
$s->bindvalue(':Author_name', $_POST['Author_name']);
$s->bindvalue(':Author_email', $_POST['Author_email']);
you are using POST
Try using same method to submit form from html code and get value in PHP code
I have two php files. index.php and insert.php. I am inserting some data by using form but I also want to store the index.php url into mysql by using html form. Because I have many pages like index.php which are calling insert.php. Actually when some page call insert.php it should take the url of that page by using form so that i can store that url into database. Code is given below.
<?php
$con=mysqli_connect("localhost","root","","commentdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Everything is working fine but not taking the url
$sql="INSERT INTO comment (id, name, email,comment,url)
VALUES
(null, '$_POST[name]','$_POST[email]','$_POST[comment]', $_POST[url])";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Comment Box</title>
</head>
<body>
<?php
//getURL function to get URL
function getURL()
{
/* First check if page is http or https */
//$whichprotocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
/* Combine different pieces of $_SERVER variable to return current URL */
//return $whichprotocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
return '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
/*Calling getURL function to display current page URL*/
?>
<form action="insert.php" method="post">
<p>Name:
<input name="name" type="text" />
</p>
<p>Email:
<input name="email" type="text" />
</p>
<p>Comment:</p>
<p>
<textarea name="comment" cols="50" rows="10"></textarea>
</p>
<p>
<--! getURL(); gives me error. It takes url but don't send it insert query -->
<input type="hidden" name="url" id="url" value="<?php getURL();?>" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
<br />
<?php
echo getURL();
?>
</body>
</html>
You need to echo the value from the function -
value="<?php echo getURL();?>" />
I have two pages: order.php and checkout.php. I have 3 items in the order page and I want to pass quantity of the items to the checkout page.
I guess the problem is with isset($_POST['Submit']). My guess is that it still goes straight to the checkout page when I press submit without putting values to session variables.
I have been trying to pass the values from order like this:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="checkout.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
And here in checkout I'm trying to print one session value as test:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
print ($_SESSION["maara1"]);
?>
</body>
</html>
"I guess the problem is with isset($_POST['Submit'])."
Yes, the problem is with if (isset($_POST['Submit']))
Your submit button is named submit instead of Submit.
<input type="submit" name="submit" value="Order"/>
Change it to
<input type="submit" name="Submit" value="Order"/>
They are case-sensitive.
Or leave it the way it is and change
if (isset($_POST['Submit']))
to
if (isset($_POST['submit']))
Either method will work. You just need to make them both (letter cases) match.
What is happening is, PHP is looking for a named element called Submit.
That alone would have and should have thrown:
Undefined index Submit...
Sidenote:
I noticed:
<input type=hidden name=price1 value=5>
and other inputs.
I would suggest that you use quotes around it:
<input type="hidden" name="price1" value="5">
while doing the same for the others, as it could have adverse effects and/or unexpected results.
I have seen that happen before.
Edit:
You'll need to move this whole block in your second page and not be in the first page.
if (isset($_POST['Submit'])) {
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
then do print ($_SESSION["maara1"]); from there.
Your first page does not recognize the POST variables because they have not been set.
Edit #2:
You could try setting a value value="{$_SESSION["maara1"]}" to your inputs.
I.e.:
Sidenote: You could try <form action="" method="post"> instead of <form action="checkout.php" method="post">
However, I'm unsure if the following is what you're looking to get. It does work if action="" but it won't work trying to get a value before it has been set. That's not how sessions work.
It's kind of like expecting an A+ in a test you haven't written yet, if I can say.
echo <<<END
<form action="checkout.php" method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" value="{$_SESSION["maara1"]}" /></label></p>
<p>Frigge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="Submit" value="Order"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['Submit'])){
$_SESSION["maara1"] = $_POST["maara1"];
$mar1 = $_SESSION["maara1"];
echo $mar1;
}
But as you said in a comment: "what is the point of me using session variables on second page then if I can refer them from $_POST anyways?"
A: Exactly.
I went and did it like this. With two button and it is also more close to webstore now. So I press first button to add the quantity of the items and at the same time it sets session variables. Then I press second button to proceed to the second page which is checkout. What you guys think?
First page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
echo <<<END
<form action="teht7.php"
method="post">
<p>Gaming Computer - 5 e/kpl <label>How many? <input type="text" name="maara1" /></label></p>
<p>Fridge - 10 e/kpl <label>How many? <input type="text" name="maara2" /></label></p>
<p>IKEA-table - 15 e/kpl <label>How many? <input type="text" name="maara3" /></label></p>
<p><input type="submit" name="submit" value="Valitse tuotteet"/></p>
</form>
<hr />
<form action="teht7_kassa.php"
method="post">
<p><input type="submit" name="submit" value="Siirry kasssalle"/></p>
<input type=hidden name=price1 value=5>
<input type=hidden name=price2 value=10>
<input type=hidden name=price3 value=15>
</form>
<hr />
END;
if (isset($_POST['submit'])) {
$ostostenmaara = $_POST["maara1"] + $_POST["maara2"] + $_POST["maara3"];
print ("Ostoskorissa on: $ostostenmaara tuotetta");
$_SESSION["maara1"] = $_POST["maara1"];
$_SESSION["maara2"] = $_POST["maara2"];
$_SESSION["maara3"] = $_POST["maara3"];
}
?>
</body>
</html>
Second page:
<?php echo '<?xml version="1.0" encoding="iso-8859-15"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">';
session_start();
?>
<html>
<head>
<title>Lomake-esimerkki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
</head>
<body>
<?php
error_reporting(E_ALL); // raportoidaan virheet
ini_set('display_errors','On'); // näytetään ne myös
$maara1 = $_SESSION["maara1"];
$maara2 = $_SESSION["maara2"];
$maara3 = $_SESSION["maara3"];
$summa = $_POST["price1"]*$maara1+$_POST["price2"]*$maara2+$_POST["price3"]*$maara3;
print ("Ostostesi yhteissumma on: $summa euroa");
?>
</body>
</html>
<?php
session_start();
$_SESSION['id']='face';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="check.php" method="post">
Admin: <input type="text" name="uname" value="<?php if(isset($_SESSION['id']) && !empty($_SESSION['id'])) { echo $_SESSION['id']; } ?>" /><br />
Password: <input type="password" name="pword" /><br />
<input type="hidden" name="login" value="1" />
<input type="submit" value="Login" />
</form>
</body>
</html>
I am working in a php language . i have made session[id] in which i have stored the the value ie face . Now in the form , ADMIN textbox , i am checking for the session[id] , if isset or not empty *echo* session[id] but if not isset or empty echo the textbox value instead of this its showing me tha number . please can any one explain me why its showing me the number .......instead of value
First, correct your html as follows:
<input type="text" name="uname" value="<?php if(isset($_SESSION['id']) && !empty($_SESSION['id'])) { echo $_SESSION['id']; } ?>" />
if you get the same error you need to check your session value before displaying the form. You can use:
var_dump($_SESSION);
to make sure if your session is not overwritten somewhere else.
There is no <input type="textbox" in html!
Use <input type="text" or <textarea> </textarea> (content goes in between)
EDIT: Session ID
If you really want to overwrite the Session ID, use the function session_id(sid). Either way, you can only set the session ID before you start the session, so your code should look like this:
session_id("face");
session_start();
I have an HTML form with three fields, first name surname and mid. name and I want to save this information in a php page. The problem is I don't know how to save multiple entries in array without a connection of database.
HTML form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Εργασία 11 - Βασίλης Τρίγκας</title>
</head>
<body>
<h4>Καταχώριση βιβλίου</h4>
<form action="form_action.php" method="get">
Συγγραφέας <br/><input type="text" name="fnwriter" /><br/>
Τίτλος <br/><input type="text" name="title" /><br/>
Εκδότης<br/> <input type="text" name="editor" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
PHP File
<?php
function display_welcome(){
print("Σωστή εισαγωγή, ");
print($_GET['fnwriter','title','editor'])
}
function display_empty_form(){
print <<<_HTML_
<FORM method="post" action="$SERVER['PHP_SELF']">
<BR/>
<INPUT type="submit" value="SUBMIT NAME">
</FORM>
_HTML_;
}
if ($_GET['fnwriter','title']) {
display_welcome();
}
else {
display_empty_form();
}
$Book=array("fnwriter","title","editor");
$fwriter=$_GET['$fnwriter']
$title=$_GET['$title']
$editor=$_GET['$editor']
echo $fnwriter;
echo $title;
echo $editor;
for ($i = 0; $i < count($fnwriter,$title,$editor); ++$i) {
echo "Book $i= $fnwriter[$i],$title[$i],$editor[$i]<br />";
}
//echo $Book[$_GET];
//print_r($);
?>
I don't really get your question, is that about saving data to a file, or about processing $_POST data?
If it's the first one, you have a plenty of possibilities, from simple CSV (or any other separator) to XML ;)