Jump to content

[SOLVED] $_POST array and $_GET ?


Schlo_50

Recommended Posts

Hello,

 

Im creating a 'select item and confirm order' script. I have items displayed on a page in the following format:

 

40266340du7.jpg

 

I have the item id and the quantity collect in their own arrays and now want to transfer that information from products.php into final.php where the user can view their selected items and then click 'finalize' which will then input the data into a database.

 

From the snippet below can someone suggest how i might go about doing the above?

 

products.php

if (isset($_POST['update']) && ($_POST['update']==="Submit Order")) {

  $orderid = array();
  $quantity = array();
  

  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];


  }
  
  $orderid = implode(',', $orderid);
  $quantity = implode(',', $quantity);

 

final.php

<?php
$_GET[$orderid] = $orderid;
$_GET[$quantity] = $quantity; 

print "$orderid";
print "quantity";
?>
<form name="the_form2" id="the_form2" method="post" action="<?php $_SERVER[php_SELF]; ?>">
<?php   print "$orderid<br />";
	print "quantity";
?>
<input name="update" type="submit" value="Submit Order" />

 

I am thinking that i need some kind of '$_GET' in final.php but have no idea how to exchange the information for the user to confirm their items are correct.

 

Any ideas? If you have any questions you need answering before you can help please do ask!

 

Thank-you

Link to comment
Share on other sites

Try to avoid passing data from page to page via POST and GET. Look at using sessions instead.

 

At the top of your scripts (be wary of any includes you use) place the following,

 

<?php
session_start();
?>

 

Now from your script you will be able to assign values to session variables and then on your other script read them in,

 

<?php

// assign values
$_SESSION['myName'] = $myvar;

//read in
if (isset($_SESSION['myName'])) {
$myvar = $_SESSION['myName'];
} else {
// create a default if session is not found
$myvar = "";
}
?>

 

These are basic examples, so read up on sessions ;)

Link to comment
Share on other sites

Ok, I forgot about sessions!

 

I took products.php and added:

 

if (isset($_POST['update']) && ($_POST['update']==="Submit Order")) {

  $orderid = array();
  $quantity = array();
  

  foreach($_POST['orderid'] as $id){
    $orderid[] = $id;
    $quantity[] = $_POST['quantity'][$id];


  }
  
  $orderid = implode(',', $orderid);
  $quantity = implode(',', $quantity);
  
  $_SESSION['quan'] = $quantity; //added
  $_SESSION['ord'] = $orderid;  //added
   
  }

 

And on final.php i used:

 

<?php   

if (isset($_SESSION['quan']) && isset($_SESSION['ord'])) {
$quantity = $_SESSION['quan'];
$orderid = $_SESSION['ord'];
}
print "$quantity<br /><br />$orderid<br />";

?>

 

The Result is 'Array'  'Array', not the values. Do i need to explode them or something?

 

Thanks for the help (Again..) lol

Link to comment
Share on other sites

As you have copied two arrays to the two session variables, they now become arrays themselves.

 

So in order to print or echo out the information you would need to iterate through the array. Something like this,

 

<?php
foreach($quantity as $quan) {
echo "{$quan}<br /><br />";
}
?>

 

Looking at your code though I have to ask why you are creating two separate arrays? I can see your data becoming a little disjointed, why not store the Order ID as the Key name and quantity as the key value?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.