Reputation: 29
I'm working on onlineshop.php page that is supposed to first ask the user about the name of his shopping cart, this is done using java script's prompt..
i will paste the code to you and explain the problem clearly
first this is the js code:
function myFunction() {
var person = prompt("Please enter Cart Name");
document.getElementById("person").value=person;
document.getElementById("form").submit();
// if (person != null) {
// document.getElementById("demo").innerHTML =
// "Your Cart Name is " + person;
//}
}
it asks the user to enter his shopping cart name. Then, there are many products in the page, each product has its own form to add to the shopping cart. this is the form's code:
<form id='form' method='post' enctype='multipart/form-data' style='display:inline;' >
<figure>
<img src='eye.jpg' alt='The Pulpit Rock' width='130' height='130' style='left:00px;top:00px; '>
<figcaption style='text-align:center;'><b>".$info['Pro_Name']."</b><br/>".$info['Price']."<p style='color:green;'>".$info['Availability']."</p><p style='font-size:13px;display:inline'>Quantity:</p>
<input type='number' name='quantity' min='1' max='10' style='width:3em;'></figcaption>
</figure>
<input type='hidden' name='Pro_ID' value=".$info['Pro_ID']." />
<input type='hidden' name='Price' value=".$info['Price']." />
<input type='hidden' name='Pro_Name' value='".$info['Pro_Name']."' />
<input type='hidden' name='Availability' value='".$info['Availability']."' />
<input type='hidden' id='person' name='cart_Name' />
<input name='button1' type='submit' value='Add To Cart' style='width:8em; ' />
</form>
I tried to make the variable person hidden so that i can use it in the query up, to insert the cart name to the database.
and here is the query:
if ( isset($_POST['Pro_ID'], $_POST['Price'] , $_POST['Pro_Name'] ) ) {
$qry="INSERT INTO shopping_cart(Cart_Name,Pro_Name,Pro_ID,Price) VALUES ( '".$_POST['person']."','".$_POST['Pro_Name']."',".$_POST['Pro_ID'].",".$_POST['Price'].")";
$result = mysql_query ($qry );
}
The error i get when i click on add to cart :
Notice: Undefined index: person in C:\xampp\htdocs\sw\onlineshop.php on line 9
Upvotes: 2
Views: 54
Reputation: 41
The name attribute should be person, if you want use that as an index in a POST operation.
Given the way your code is now, the index name in the query should be "cart_name"
Upvotes: 0
Reputation: 360862
Spot the differences:
document.getElementById("person").value=person;
^^^^^^--- your ID
<input type='hidden' id='person' name='cart_Name' />
your ID ---^^^^^^^^^^^ ^^^^^^^^^^---what gets submitted to the server
You want want $_POST['cart_Name']
. The JS-side variable names/IDs are utterly irrelevant when it comes time to submit the form. Only the name
is used.
And note that you're vulnerable to SQL injection attacks. please sit back and relax. Your server will be pwn3d very soon, rendering your problem moot.
Upvotes: 3