return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>php - Transfer data from onClick into $_POST - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Transfer data from onClick into $_POST - Stack Overflow

programmeradmin3浏览0评论

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 mit 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

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 mit 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

Share Improve this question edited Nov 18, 2011 at 21:59 Andrew asked Nov 18, 2011 at 21:07 AndrewAndrew 2331 gold badge3 silver badges10 bronze badges 2
  • 1 Basically you want to tell the server which of those 3 links was clicked on? – Marc B Commented Nov 18, 2011 at 21:10
  • Basically - yes yes yes :)) I have one long 7 scripts (one for every day of the week) at one php file. I think to reduce it to one script but I must inform php file about which day (1,2,3,4,5,6,7) we are tolking. – Andrew Commented Nov 18, 2011 at 21:16
Add a ment  | 

2 Answers 2

Reset to default 4

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>

use jQuery

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

评论列表(0)

  1. 暂无评论