Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过锚点将值传递给ajax。_Php_Ajax - Fatal编程技术网

Php 通过锚点将值传递给ajax。

Php 通过锚点将值传递给ajax。,php,ajax,Php,Ajax,我试图将一个值传递给ajax函数。这在表单上很简单,并按预期工作(在下面的示例中将id更改为gal_id1),但是我在为锚传递值时遇到了未定义的错误 <html> <head> <script> function ajax_post(){ // Create our XMLHttpRequest object var hr = new XMLHttpRequest(); // Create some variables we need

我试图将一个值传递给ajax函数。这在表单上很简单,并按预期工作(在下面的示例中将id更改为gal_id1),但是我在为锚传递值时遇到了未定义的错误

<html>
<head>
<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "ajax_json_gallerySQL.php";
    var gid = document.getElementById("gal_id").value;
    var vars = "gal_id="+gid;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("status").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>

<body>
<h2>Test Pictures. </h2>

THis is working
<input id="gal_id1" name="gal_id1" type="text">  <br><br>
<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();" > <br><br>
<div id="status"></div>
<?php 

$myValue='13';
This is not working
echo "<td> <a id='gal_id' name='gal_id' href='#' onClick='ajax_post();' value='13' ><img src='images/Parents/Brook/Brook1.jpg' alt='Image' width='270' height='203'></a><br><br>";

 ?>          


</body>
</html>

函数ajax_post(){
//创建XMLHttpRequest对象
var hr=新的XMLHttpRequest();
//创建一些我们需要发送到PHP文件的变量
var url=“ajax\u json\u gallerySQL.php”;
var gid=document.getElementById(“gal_id”).value;
var vars=“gal_id=“+gid;
hr.open(“POST”,url,true);
//设置用于在请求中发送url编码变量的内容类型标头信息
hr.setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
//访问XMLHttpRequest对象的onreadystatechange事件
hr.onreadystatechange=函数(){
如果(hr.readyState==4&&hr.status==200){
var return_data=hr.responseText;
document.getElementById(“status”).innerHTML=返回数据;
}
}
//立即将数据发送到PHP…并等待更新status div的响应
hr.send(vars);//实际执行请求
document.getElementById(“status”).innerHTML=“处理…”;
}
测试图片。
这是有效的





锚点的值与输入的值不同,因此
.value
不起作用。您可以改为使用
document.getElementById(“gal_id”).getAttribute(“value”)

话虽如此,您可能应该使用数据属性,例如


没有为标记定义值。因此无法获取#gal_id的值。请尝试添加数据属性,以便获取gal_id。