Jump to content

How to transmit data between two pages


mostafatalebi

Recommended Posts

hello

 

I have a php page, which gets several rows of data from a mysql table, and arrange each row's data(fields) in one div, and renders them:

 

an example: the div include a place for : name_of_customer, price, and a button.

 

Then when I have four orders in the table (which means literally four rows), I get for divs rendered. Which means four name_of_customer, four price and four button all with the same names. the when I select the button I want to transmit only and only the selected div's information. I can't simply declare:

 

$_SESSION["name_of_customer"] = $name_of_customer //

 

because there are four $name_of_customer;

 

How shall I solve this?

Link to comment
Share on other sites

If you must use sessions, just assign an empty session array to loop across each div and session it accordingly.

 

E.g

 

$_SESSION['customer_names'] = array();
foreach ($name_of_customers AS $name_of_customer)
{
   echo '<div>'. $name_of_customer .'</div>';
  $_SESSION['customer_names'] [ ] = $name_of_customer
}

 

You can always retreive your session contents in the next page by accessing the $_SESSION['customer_names'] array.

 

e.g

if (!empty($_SESSION['customer_names']) {

   foreach ($_SESSION['customer_names'] AS $names)
   {
       echo '<div>'. $names .'</div>';
   }
}

Link to comment
Share on other sites

But it does not seem to work (I have not tried it yet) My issue is that how I can transmit the one div that its button is clicked by the user? something like this: We have three divs generated by a php code out of fetching a sql table's data, the middle one is clicked, and I need to distinguish it from the other. How then?

 

<div id="info">
 <input type="submit" name="send" />
</div>
<div id="info">
 <input type="submit" name="send" />
</div>
<div id="info">
 <input type="submit" name="send" />
</div>

Link to comment
Share on other sites

I would personally go with Jessica's solution, but it is also achievable via Javascript which has its drawbacks.

 

I have had to use the following method once before:

<?PHP

 if($_SERVER['REQUEST_METHOD'] == 'POST') {

   //### Set action array
   $actionArray = array(1 => 'first',
					 2 => 'second',
					 3 => 'third');

   //### Set posted action and cast it as an integer				     
   $postedAction = (int)$_POST['action'];

   //### Check the posted action exists
   if(isSet($actionArray[$postedAction])) {
  echo 'Selected Action: '. $actionArray[$postedAction];
   } else {
  echo 'No selected action.';
   }
 }

?>

<form method="POST" action="?">
 <input type="hidden" name="action" id="action">
 <div id="info">
   <input type="submit" name="send" onclick="which(1);">
 </div>
 <div id="info">
   <input type="submit" name="send" onclick="which(2);">
 </div>
 <div id="info">
   <input type="submit" name="send" onclick="which(3);">
 </div>
</form>

<script type="text/javascript">
 function which(action) {
   document.getElementById('action').value = action;
 }
</script>

Link to comment
Share on other sites

I use the id for each row. and add them to the end of the name for each of the divs. now I have for instance one hundred divs incrementally named. then How do I understand which div's button is clicked (the problem is not the names, is understanding which div. I need something like Javascript to write the clicked button's div's name into a .xml node and then retrieve that written name using php. Something Like this.

Edited by mostafatalebi
Link to comment
Share on other sites

No, you don't. This is very simple. Going back and reading your original post it's very clear. 

 

hello

 

I have a php page, which gets several rows of data from a mysql table, and arrange each row's data(fields) in one div, and renders them:

 

an example: the div include a place for : name_of_customer, price, and a button.

 

Then when I have four orders in the table (which means literally four rows), I get for divs rendered. Which means four name_of_customer, four price and four button all with the same names. the when I select the button I want to transmit only and only the selected div's information. 

 

What you need to do is add a hidden input with the row's ID in it. Now you know what row you're trying to submit. End of story. (Each row needs to have it's own form, btw.)

Edited by Jessica
Link to comment
Share on other sites

edit: What Jessica said, basically.  there's an example.

--

 

Unless your HTML/Form layout somehow prevents it, you could just do a separate form for each set also:

<form action="blah">
  <input type="hidden" name="name_of_customer" value="blah">
  ...other hidden inputs for each detail
  <input type="submit" value="Send">
</form>
<form action="blah">
  <input type="hidden" name="name_of_customer" value="foo">
  ...other hidden inputs for each detail
  <input type="submit" value="Send">
</form>
<form action="blah">
  <input type="hidden" name="name_of_customer" value="bar">
  ...other hidden inputs for each detail
  <input type="submit" value="Send">
</form>

 

Then the only details that would be submitted with the form are the one set, not all four.

Edited by kicken
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.