简体   繁体   English

PHP \\ MySQL \\ AJAX-动态字段

[英]PHP \ MySQL \ AJAX - dynamic fields

I'm having trouble passing different variables to my javascript function. 我在将不同的变量传递给javascript函数时遇到麻烦。 Here goes: 开始:

I have PHP code that basically builds out rows of data. 我有基本上可以构建数据行的PHP代码。 What I want to do is being to save each row separately via an AJAX call. 我要做的是通过AJAX调用分别保存每一行。 Here's what I have so far. 到目前为止,这就是我所拥有的。 What is happening is that the first row works fine, but all the subsequent rows do not (the javascript variables are the ones from the first row). 发生的情况是第一行工作正常,但随后的所有行均不工作(javascript变量是第一行中的javascript变量)。

FRONT END PHP CODE 前端PHP代码

 <?php 
  $result = mysql_query("SELECT * FROM scoresheet WHERE matchup_id = '$matchupid' AND   team_id = '$teama' AND status = '1' "); 
  $num_rows = mysql_num_rows($result);
  if ( mysql_num_rows($result) == 0 ) { echo "<div style='float:left;clear:both;'>Nothing found</div>"; } else {
while($row = mysql_fetch_array($result)) 
{
echo "  <form name='input'>";    
echo "  <div class='tablecell'>".$row['full_name']."</div>";
echo "  <div class='tablecell'>".$row['scoresheet_id']."</div>";
echo "  <input type='hidden' id='scoresheet_id' name='scoresheet_id' value='".$row['scoresheet_id']."'></input>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='presenta' name='presenta' value='".$row['present']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='sparea' name='sparea' value='".$row['spare']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='goaliea' name='goaliea' value='".$row['goalie']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='goalsa' name='goalsa' value='".$row['goals']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='assistsa' name='assistsa' value='".$row['assists']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='yellowa' name='yellowa' value='".$row['yellow']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='reda' name='reda' value='".$row['red']."'></input></div>";
echo "  <input type='button' class='btnInput'  style='float:left;margin-top:-2px;' onClick='updatescore()' value='Save'></input>";
}    
}
?>

JAVASCRIPT CODE JAVASCRIPT代码

function updatescore() {
    var presenta = document.getElementById('presenta').value;
    var sparea = document.getElementById('sparea').value;
    var goaliea = document.getElementById('goaliea').value;
    var scoresheet_id = document.getElementById('scoresheet_id').value;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtuser").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "testajax-x.php?presenta="+presenta+"&sparea="+sparea+"&goaliea="+goaliea+"&scoresheet_id="+scoresheet_id, true);
    xmlhttp.send();
}

Well, your problem is that you are going to be echoing out a bunch of forms with the same ID's For this to work properly, each input item must have it's own ID. 好吧,您的问题是,您将回显一堆具有相同ID的表单。为了使此表单正常工作,每个输入项都必须具有自己的ID。

Your javascript lines: 您的javascript行:

var presenta = document.getElementById('presenta').value;
var sparea = document.getElementById('sparea').value;
var goaliea = document.getElementById('goaliea').value;
var scoresheet_id = document.getElementById('scoresheet_id').value;

Are automatically going to only select the FIRST elements with those id's that they come across. 会自动选择遇到其ID的FIRST元素。 So you need to make the increment id's like presenta1, presenta2, etc. on each loop iteration. 因此,您需要在每次循环迭代中使增量id像presenta1,presenta2等。 And then you need to reference the increment value in your call to updatescore() . 然后,您需要在调用updatescore()引用增量值。

You might try something like this: 您可以尝试如下操作:

$i=1;
while($row = mysql_fetch_array($result)) 
{
echo "  <form name='input'>";    
echo "  <div class='tablecell'>".$row['full_name']."</div>";
echo "  <div class='tablecell'>".$row['scoresheet_id']."</div>";
echo "  <input type='hidden' id='scoresheet_id' name='scoresheet_id' value='".$row['scoresheet_id']."'></input>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='presenta{$i}' name='presenta' value='".$row['present']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='sparea{$i}' name='sparea' value='".$row['spare']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='goaliea{$i}' name='goaliea' value='".$row['goalie']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='goalsa{$i}' name='goalsa' value='".$row['goals']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='assistsa{$i}' name='assistsa' value='".$row['assists']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='yellowa{$i}' name='yellowa' value='".$row['yellow']."'></input></div>";
echo "  <div class='labelAnswer'><input class='standardscore' type='textfield' id='reda{$i}' name='reda' value='".$row['red']."'></input></div>";
echo "  <input type='button' class='btnInput'  style='float:left;margin-top:-2px;' onClick='updatescore({$i})' value='Save'></input>";
$i++;
}

Note you well then need to modify your javascript to add the increment value to the getElementById selectors like: 请注意,然后您需要修改JavaScript,以getElementById量值添加到getElementById选择器中,例如:

function updatescore(id) {
    var presenta = document.getElementById('present'+id).value;
    // and so on.

Finally, you would need to add the row id value to the ajax call so that you know which row in the DB to update. 最后,您需要将行ID值添加到ajax调用中,以便您知道要更新数据库中的哪一行。

You might even be better served using the row id from the database (if you have like an autoincrement value on that table) than the $i increment variable I am showing, in that this would give you a direct way to tie back each HTML row to the AJAX call via the javascript. 与我显示的$i增量变量相比,使用数据库中的行ID(如果您在该表上具有自动增量值)甚至可能会得到更好的服务,因为这将为您提供直接绑定每个HTML行的方式通过javascript转到AJAX调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM