russthebarber Posted July 31, 2011 Share Posted July 31, 2011 I have a table that lists rows of data from a mysql database using a foreach loop in php. Each row has an edit button. Normally I would click on the edit button and this would go to a page where the specific row could be edited. Something like this: <td><a href="edit_entry.php?id=<?php echo $entry->id; ?>"><img src="../images/edit.gif" border=0></a></td> This time, however, what I want is that all this is done without going to another page. I can use javascript and/or jQuery to open a window on my page with a form for editing, but I am not sure how it would be known which id edit button was clicked. Does anyone know of a sensible way to do this. maybe something in the php foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/ Share on other sites More sharing options...
phpSensei Posted July 31, 2011 Share Posted July 31, 2011 Are you talking about Ajax? If i am correct, then you pass the variable into a function which sends a HTTP REQUEST to another page which handles the ID and returns it in XML or whatever format you want, then you can update the current page. nomsayin? Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1249910 Share on other sites More sharing options...
russthebarber Posted August 1, 2011 Author Share Posted August 1, 2011 Shame this post has been moved to the ajax section as I am not sure I need ajax or anything very complicated for this. All my rows information is stored already in an array. I was thinking that somehow the foreach loop could assign an id to each edit button and then when clicked the id could be used to show a particular row. Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1250085 Share on other sites More sharing options...
mitty Posted August 1, 2011 Share Posted August 1, 2011 You could make each item its own form, that way you have multiple submit buttons. <form id = '0'> <data> <submit button> </form> <form id = '1'> <data> <submit button> </form> .... Then you will need to use AJAX so that only part of the page gets changed. Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1250091 Share on other sites More sharing options...
trq Posted August 1, 2011 Share Posted August 1, 2011 Shame this post has been moved to the ajax section as I am not sure I need ajax or anything very complicated for this. If you want any type of editing to be done without refreshing the page you will need to use Ajax to make the request to the server in the background. As for the issue at hand, I'm not exactly sure where you are stuck. I can use javascript and/or jQuery to open a window on my page with a form for editing, but I am not sure how it would be known which id edit button was clicked In jQuery, the clicked object would shows up as within the callback as this. Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1250124 Share on other sites More sharing options...
russthebarber Posted August 1, 2011 Author Share Posted August 1, 2011 I'm pretty sure the problem is that my explaination is not good enough so I'll try and explain another way: I already have rows information from my database table stored in an array called "$entries" when the page first loads. Therefore I don't need to get anything from the server or reload the page at all to list the information that I need. I go through the array in a php foreach loop and list out my row info onto an html table. <?php foreach($entries as $entry) { ?> <tr> <td><?php echo ucwords($entry->firstName); ?></td> <td><?php echo ucwords($entry->lastName); ?></td> <td><?php echo ucwords($entry->pseudonym); ?></td> <td><?php echo ucwords($entry->company); ?></td> <td><?php echo $entry->tel; ?></td> <td><?php echo $entry->mobile; ?></td> <td><?php echo $entry->email; ?></td> <td><?php echo ucwords($entry->town); ?></td> <td><?php echo ucwords($entry->country); ?></td> <td><img id="<?php echo $entry->id; ?>" src="../images/edit.gif" border=0></td> </tr> <?php } ?> On each row you can see on the last line that an edit button is created. If the user clicks the edit button for id=15 for example, I want to then list out in a separate window (javascript / jquery) the row information for id=15. As this information is already in my php arry "$entries", there must be a way of doing this without reloading the page or using ajax. Anyone know the answer? Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1250151 Share on other sites More sharing options...
trq Posted August 1, 2011 Share Posted August 1, 2011 As this information is already in my php arry "$entries", there must be a way of doing this without reloading the page or using ajax. Anyone know the answer? But the $entries array is a php array which has already been created and long forgotten by the time your page has been created. You could put the contents of your $entries array into hidden divs (when the page is created) and have Javascript un-hide these divs when the appropriate link is clicked. Does this sound like what you want? Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1250155 Share on other sites More sharing options...
russthebarber Posted August 1, 2011 Author Share Posted August 1, 2011 thorpe, I see what you mean. Thanks. Maybe I could even put the php array information into javascript arrays when the page is created and then do the whole thing in javascript. Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1250158 Share on other sites More sharing options...
trq Posted August 1, 2011 Share Posted August 1, 2011 thorpe, I see what you mean. Thanks. Maybe I could even put the php array information into javascript arrays when the page is created and then do the whole thing in javascript. You could depending on the amount of data. I would be more inclined to go down the Ajax path. When a link is clicked you send the id back to the server, executing a script to get the data, then send it back to the client as a json object. Quote Link to comment https://forums.phpfreaks.com/topic/243407-using-php-id-without-going-back-to-server/#findComment-1250165 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.