Jump to content

Recommended Posts

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?

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?

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.

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.

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.

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?

 

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?

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.

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.