mostafatalebi Posted February 12, 2013 Share Posted February 12, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/ Share on other sites More sharing options...
daviddivine Posted February 12, 2013 Share Posted February 12, 2013 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1411973 Share on other sites More sharing options...
mostafatalebi Posted February 12, 2013 Author Share Posted February 12, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412023 Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 (edited) Give them different names and/or values. Once you click do a print_r($_POST) to see what happens. Edited February 12, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412028 Share on other sites More sharing options...
PaulRyan Posted February 12, 2013 Share Posted February 12, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412030 Share on other sites More sharing options...
mostafatalebi Posted February 12, 2013 Author Share Posted February 12, 2013 you know the problem is that I don't know how many divs are. they are generated based on a dynamic user-filled (minute-by-minute) database tables. So there may be one hundred divs generated. I need a super procedural way to catch the name of the div. How? Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412037 Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 Use the id for each row. Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412038 Share on other sites More sharing options...
mostafatalebi Posted February 12, 2013 Author Share Posted February 12, 2013 (edited) 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 February 12, 2013 by mostafatalebi Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412043 Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 (edited) 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 February 12, 2013 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412045 Share on other sites More sharing options...
mostafatalebi Posted February 12, 2013 Author Share Posted February 12, 2013 Hey Jessica Thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412050 Share on other sites More sharing options...
kicken Posted February 12, 2013 Share Posted February 12, 2013 (edited) 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 February 12, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/274391-how-to-transmit-data-between-two-pages/#findComment-1412094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.