调用Ajax的功能在PHP的foreach表单 [英] Call Ajax function from PHP foreach with form

查看:119
本文介绍了调用Ajax的功能在PHP的foreach表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题在这里。我想打电话给从PHP foreach循环一个Ajax功能 这是我的code:

i have a little Problem here. I want to call an Ajax function from a PHP foreach Loop Here is my code:

阿贾克斯:

 function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!

 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var bla = document.getElementById('bla').value;
 var queryString = "?bla=" + bla;
 ajaxRequest.open("GET", "ajax-example.php" + 
                              queryString, true);
 ajaxRequest.send(null); 
}

和PHP的code:

foreach ($data AS $key => $value){
    <form name="myForm">
    <input type="text" id="bla" name="bla" value="<?php echo $value['bla']; ?>">
    <input type="button" onclick="ajaxFunction(<?php echo $value['id']; ?>)" value="Speichern">
    </form>
}

问题:当我点击foreach循环的只有第一个结果出现在输出foreach循环所生成的几个按钮。我想我必须提交由foreach循环(从数据库)的单独的ID Ajax的功能,但我不知道如何做到这一点。

The Problem: when I click the several buttons generated in the foreach loop only the first result of the foreach loop appears in the output.. I think I have to commit the individual ID from the foreach loop (from database) to the Ajax function but I dont know how to do this.

也许这样调用该函数?功能ajaxFunction(ID)?!

Maybe call the function like this? function ajaxFunction(id) ?!

推荐答案

在你的PHP foreach循环,你分配相同的ID到每个文本输入框。当你调用的getElementById(),它将总是返回的第一个元素与DOM和变量中的ID喇嘛将被分配的第一个元素的值属性。

Within your PHP foreach loop, you're assigning the same ID to each of your text inputs. When you call getElementById(), it will always return the first element with that ID within the DOM and the variable "bla" will be assigned the value attribute of that first element.

看起来你可能一直在试图传递一个ID为onclick属性内的ajaxFunction调用,所以也许你可以做到以下几点:

It looks like you may have been trying to pass in an ID into the ajaxFunction call within the onclick attribute, so perhaps you could do the following:

1)把文本输入的id属性在同一个echo语句。

1) Place the same echo statement within the text input's id attribute.

&LT;输入类型=文本ID =&LT; PHP的echo $值['身份证'];?&gt;中NAME =喇嘛值=&LT; PHP的echo $值['喇​​嘛'];?&gt;中&GT;

2)一个参数添加到ajaxFunction定义。

2) Add a parameter to the ajaxFunction definition.

函数ajaxFunction(requestedID){

3)并将其传递变量到的getElementById电话。

3) And pass that variable into the getElementById call.

VAR喇嘛=的document.getElementById(requestedID).value的;

这篇关于调用Ajax的功能在PHP的foreach表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆