qazerty23 qazerty23 - 1 year ago 114
PHP Question

Link click and button click in one HTTP request

I was developing on my localhost and came across this question.

Consider there is such HTML code:

<form action="" method="POST">
<input type="submit" name="submitButton" id="submitButton">

<a onclick="clickButton('submitButton');" href="guestbook/index.php?pageNumber=1">

click triggers submit button click event. The Javascript function looks like this:

function clicktButton(id) {
if (id != "")
document.getElementById(id).click(); // this will trigger the click event

My question is: is something like this possible? To send a HTTP request with link press and button press simultaniously?

Because by pressing an
link I already send a HTTP request with
parameter. But I also want to send the
data at the same time.

Thanks in advance!

Answer Source

What you want is possible, but not so much with the code I see before me..

<form action="guestbook/index.php?pageNumber=1" method="post">
    <input type="submit" name="submitButton" id="submitButton">
    <input type="button" name="submit">

This would essentially post the contents of the form, with a get request attached.

An <a> would not really work, because it generates a new HTTP request and would presumably skip JavaScript execution of the currently loaded page.

On the other hand, you can simulate the effects of an <a> click with JavaScript, by making a post request with the form data to guestbook/index.php?pageNumber=1. But if you do this I would recommend jQuery as it will make things easier for you by adding this to the click event.

    url:   "guestbook/index.php?pageNumber=1",
    data:  {'field':'value'},
    async: false
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download