Skip to content Skip to sidebar Skip to footer

Do Something Different Depending On What The User Clicks On

I have three anchor links: go 1go 2go 3

Solution 1:

As far as I know you can't do that using only PHP.

What I mean is that you're trying to understand what link has been pressed, which is nothing that has to do with the form itself.

To accomplish this, I would suggest you to use javascript OR jQuery to set an hidden field inside the form and, then, send it.

Example:

Link:

<a href="#test" name="test">link</a>

Form:

<divid="signup"><formaction="send_form.php"method="post"><inputtype="hidden"name="linkPressed" /><inputname="text"type="text" /><buttontype="submit">Send</button></form></div>

javascript (assuming jQuery):

$('a').click(function(){
  $('#linkPressed').val($(this).attr('name'));
});

send_form.php:

$pressedlinkname = $_POST['linkPressed'];

And there you are.

Don't take it as the exact solution of your problem: inspire your solution by following this.

Hope this helps.

Solution 2:

You could not know this simply from PHP. This is something you would find out by using javascript (or better yet, jQuery).

The approach would be to:

  • trap the click event on all anchor tags
  • assign the name (or better yet, ID) to a variable
  • add the variable's ID to a hidden field in the form

This is what it would look like using jQuery:

jsFiddle demo

HTML:

<a name="send1"id="send1" href="#signup">go 1</a>
<a name="send2"id="send2" href="#signup">go 2</a>
<a name="send3"id="send3" href="#signup">go 3</a>

<div id="signup">
    <form action="send_form.php">
        <input name="text"type="text">
        <input name="hidden_field"id="hidden_field"type="hidden" />
        <button type="submit">Send</button>
    </form>
</div>

javascript:

var the_anchor;

$('#signup').dialog({
    autoOpen:false,
    close: function(){
        $(this).dialog('close');
    }
});

$('a').click(function() {
    the_anchor = $(this).attr('id');
    $('#hidden_field').val(the_anchor);
    $('#signup').dialog('open');
});

Note that if you are using jQuery, you must reference the jQuery libraries in the <head> tags of the document:

<head><scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><linkhref="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/flick/jquery-ui.css" /><scriptsrc="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script></head>

Solution 3:

The hash (#) portion of a URL is used on the client side only. It will never appear in server logs, requests, or as part of a REFERRER string. You have to send it via FORM (using javascript) eg.:

<input name="hash"type="text">

and change the value of input using JavaScript

Solution 4:

Javascript!

//html
<aname="send1"href="#signup"onclick="myFunction(1)>go 1</a>
<a name="send2" href="#signup"onclick="myFunction(2)>go 2</a>
<a name="send3" href="#signup"onclick="myFunction(3)>go 3</a>

<div id="signup"><formaction="send_form.php"><inputname="text"type="text"><inputid="clicked"type="hidden"value=0><buttontype="submit">Send</button></form></div> 
//js
function myFunction(x){
   document.getElementById("clicked").value = x;
} 

Solution 5:

First, add a hidden input in your form:

<inputtype="hidden"id="clicked_link" name="clicked_link" value="" />

Then use some jquery:

$(document).ready(function(){
    $('a').click(function() {
       $('#clicked_link').val($(this).attr('name'));
    });
});

Or you could use pure javascript I suppose, but it is similar approach..

Post a Comment for "Do Something Different Depending On What The User Clicks On"