Andrew
Andrew

Reputation: 233

Transfer data from onClick into $_POST

I call some links (opening table in the div) in the form

<A HREF="#1" onClick = ?????>button 1<A>
<A HREF="#2" onClick = ?????>button 2<A>
<A HREF="#3" onClick = ?????>button 3<A>

I would like through the onClick function to send data (numbers: 1, 2, 3) and receive it in PHP in the same document. I guess I have to commit to this form. How to do it?

EDIT -------------------------------------

I try the @gilly3 way

<script language="JavaScript">
function submitValue (n) {     
var f = document.forms.myform_1;     
f.myNumber.value = n;     
f.submit(); 
} 
</script>

<?php
global $PHP_SELF;
echo "<form action='". htmlentities($PHP_SELF)."' method=\"POST\" id=\"myform_1\">";
?>
<input type="hidden" name="myNumber" /> 
<a href="#1" onclick="submitValue(1)">button 1</a> 
<a href="#2" onclick="submitValue(2)">button 2</a> 
<a href="#3" onclick="submitValue(3)">button 3</a>
</form>

tested - working ok. thnks for your help

Upvotes: 3

Views: 8528

Answers (2)

tmjam
tmjam

Reputation: 1039

use jQuery

$('#anchorid').click(function(){
   $.post('self.php',{data:'1'},function(){
    }); 
});

Upvotes: 0

gilly3
gilly3

Reputation: 91497

Add hidden fields to your form. In your click handler, write whatever value you want to the hidden fields and call form.submit().

function submitValue (n) {
    var f = document.forms.myForm;
    f.myNumber.value = n;
    f.submit();
}

Use it like this:

<input type="hidden" name="myNumber" />
<a href="#1" onclick="submitValue(1)">button 1</a>
<a href="#2" onclick="submitValue(2)">button 2</a>
<a href="#3" onclick="submitValue(3)">button 3</a>

Or get the value from $_GET and skip the JavaScript:

<a href="?day=1">button 1</a>
<a href="?day=2">button 2</a>
<a href="?day=3">button 3</a>

Upvotes: 4

Related Questions