Hi to you guys,
I've just been trying to sort this out...I know how to prevent a page load / refresh and I know how to get PHP ID from a html href link...but I have no clue how I can get the value and prevent the page from reloading at the same time...In other words how to assign a function to a PHP link like this:

This doesn't refresh and it's fine:

echo "<a href='#' /></a>";

..but in order to assign ID do I have to insert a PHP page ?

echo "<a href='index.php?id=$value'/></a>";

So how to prevent the page from reloading while getting id...Thanks for comments.

Recommended Answers

All 7 Replies

I don't understand

I don't understand

Ok - to be more concise...I am assinging an ID in a href link that wraps a button like this:

echo"<a href='index.php?id=$value'/><img src='button.jpg' id='button' /></a>";

and...I get this ID in another PHP page when I click the button:

$val = $_GET['id'];

...but when I click it - the page refreshes. How can I stop it from refreshing and still get the ID value that is in a href ?

or - is there any other way I can assing $value to an ID in similar way ? Or how to call JS function to stop the refresh while still getting ths ID.

Member Avatar for diafol

clicking a link will take you to that page. you set the href property yourself, so I thought that would be obvious.
If you want to get data from the page, use js. if you want data from the page and get it processed on the server and returned, use ajax.

You can't get the id value from the link url without going there, unless, as I mentioned you use JS.

You still haven't mentioned why you need to do this. Seems a bit odd.

clicking a link will take you to that page. you set the href property yourself, so I thought that would be obvious.
If you want to get data from the page, use js. if you want data from the page and get it processed on the server and returned, use ajax.

You can't get the id value from the link url without going there, unless, as I mentioned you use JS.

You still haven't mentioned why you need to do this. Seems a bit odd.

Hi, first of all, there's nothing odd in here...You have a button that works as a link. The button works as a delete button for removing users' posts. The variable holds the IDs of those posts. When you click on the button it removes the comment and also calls a javascritp function that uses Ajax - that's why I wanted the page to stop reloading and get the ID from the link for a PHP page that JS ajax calls...

Member Avatar for diafol

1. With respect, it's not a button, it's an image.
2. Ajax links don't need a href url, they usually use a '#', although some people use 'javascript: ....'
3. You can hijack the link's behaviour with an event handler, so you could use this as progressive enhancement. If js not available, the link will function as normal, ie go to the url.
4. To use ajax on a link, you can use onclick attribute or even write an event handler. If you have a list of delete images/links, use an event handler. This is the code that will link to the php script to update the DB.

The delete image link should have some id value (or pick a different attribute) associated with it, e.g.

<a href="#" id="del_45" name="del[45]" class="delete_image"><img ... /></a>

The event handler (using class delete_image) then waits for a click. You can use 'this' to pull the id or the name to pass on the info to your php script.
Ensure that you have sessions or something for this as this type of deletion request is 'open'.

1. With respect, it's not a button, it's an image.
2. Ajax links don't need a href url, they usually use a '#', although some people use 'javascript: ....'
3. You can hijack the link's behaviour with an event handler, so you could use this as progressive enhancement. If js not available, the link will function as normal, ie go to the url.
4. To use ajax on a link, you can use onclick attribute or even write an event handler. If you have a list of delete images/links, use an event handler. This is the code that will link to the php script to update the DB.

The delete image link should have some id value (or pick a different attribute) associated with it, e.g.

<a href="#" id="del_45" name="del[45]" class="delete_image"><img ... /></a>

The event handler (using class delete_image) then waits for a click. You can use 'this' to pull the id or the name to pass on the info to your php script.
Ensure that you have sessions or something for this as this type of deletion request is 'open'.

Yes, thank you, of course image not a button as you said ( wrote it late at night ). I know JS doesn't need href. The point was: <a href='index.php?id=$value'> is passing them when you click the image that also calls JS. Ajax calls a php script that deletes the entries based on those values from <a href> and this php script is included in index.php.
So I wanted to stop the refresh when the image is pressed and ajax is called. I know a bit intricate so I've decided to re-program this now...but thanks a lot.

P.S. ...also the id or name need to pass a dynamic value from php to a href from a while loop. I just haven't included all script.

Yes, thank you, of course image not a button as you said ( wrote it late at night ). I know JS doesn't need href. The point was: <a href='index.php?id=$value'> is passing them when you click the image that also calls JS. Ajax calls a php script that deletes the entries based on those values from <a href> and this php script is included in index.php.
So I wanted to stop the refresh when the image is pressed and ajax is called. I know a bit intricate so I've decided to re-program this now...but thanks a lot.

P.S. ...also the id or name need to pass a dynamic value from php to a href from a while loop. I just haven't included all script.

id should be added during creation of contents/comments from server. for example if your comments are in a div then add id of a button/image the same as id of the comment in database. Catch onclick events for all buttons/images, take an id and send it to delete.php (or whatever delete comments) via ajax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.