“`plaintext php – 在不重新加载页面的情况下使用ajax更新记录 “`

huangapple go评论66阅读模式
英文:

php - ajax Update record onchange wihout reload page

问题

以下是您要翻译的内容:

I need create a table, in the column the size and in the row the quantity for every product, I create a query where put in array the different result to have column and row and after I create a table.. I need to update every time there anybody do a change

while($xx < count($testa))
{
    $testaecho .= "<th>".$testa[$xx]."</th>";
    $corpoecho .= "<td style=' max-width: 20px;'>";
    $corpoecho .= "<form name='changeqta' method='post' action=''>";
    $corpoecho .= "<input type='number' maxlength='3' id='mod' value='".$corpo[$xx]."'>";
    $corpoecho .= "<input type='hidden' id='ctgr' value='".$idriga[$xx]."'>";
    $corpoecho .= "</form>";
    $corpoecho .= "</td>";
    $xx++;
}

echo "<tr>".$testaecho."</tr>";
echo "<tr>".$corpoecho."</tr>";

and in the jquery

<script>
$(document).ready(function(){
$('mod').on('change', function(){
var name=$("mod").val();
var ctgr=$("ctgr").val();
$.ajax({
url:'update.php',
method:'POST',
data:{
name:name,
ctgr:ctgr
},
success:function(response){
alert(response);
}
});
});
});
</script>
the problem is only the first input do the change, if i change all other input, the firs product or the other product not update... is the same that the second "mod" is not do the submit

请注意,这些代码包含了一些HTML和PHP,以及使用jQuery进行的JavaScript操作。如果您需要有关代码的解释或帮助,可以提出具体问题。

英文:

I need create a table, in the column the size and in the row the quantity for every product, I create a query where put in array the different result to have column and row and after I create a table.. I need to update every time there anybody do a change

while($xx &lt; count($testa))
	{
		$testaecho .= &quot;&lt;th&gt;&quot;.$testa[$xx].&quot;&lt;/th&gt;&quot;;
		$corpoecho .= &quot;&lt;td style=&#39; max-width: 20px;&#39;&gt;&quot;;
		$corpoecho .= &quot;&lt;form name=&#39;changeqta&#39; method=&#39;post&#39; action=&#39;&#39;&gt;&quot;;
		$corpoecho .= &quot;&lt;input type=&#39;number&#39; maxlength=&#39;3&#39; id=&#39;mod&#39; value=&#39;&quot;.$corpo[$xx].&quot;&#39;&gt;&quot;;
		$corpoecho .= &quot;&lt;input type=&#39;hidden&#39; id=&#39;ctgr&#39; value=&#39;&quot;.$idriga[$xx].&quot;&#39;&gt;&quot;;
		$corpoecho .= &quot;&lt;/form&gt;&quot;;
		$corpoecho .= &quot;&lt;/td&gt;&quot;;
		$xx++;
	}

   echo &quot;&lt;tr&gt;&quot;.$testaecho.&quot;&lt;/tr&gt;&quot;;
   echo &quot;&lt;tr&gt;&quot;.$corpoecho.&quot;&lt;/tr&gt;&quot;;

and in the jquery

&lt;script&gt;
$(document).ready(function(){
	$(&#39;#mod&#39;).on(&#39;change&#39;, function(){
		var name=$(&quot;#mod&quot;).val();
		var ctgr=$(&quot;#ctgr&quot;).val();
		$.ajax({
			url:&#39;update.php&#39;,
			method:&#39;POST&#39;,
			data:{
				name:name,
				ctgr:ctgr
			},
			success:function(response){
				alert(response);
			}
		});
	});
});

</script>

the problem is only the first input do the change, if i change all other input, the firs product or the other product not update... is the same that the second "mod" is not do the submit

why?

答案1

得分: 1

由于不允许重复的ID,尝试使用类。而且,尝试使用属性而不是另一个隐藏输入。例如,

$corpoecho .= "<input type='number' maxlength='3' class='mod' data-mod='" . $idriga[$xx] . "' value='" . $corpo[$xx] . "'>";

检查 data-mod='" . $idriga[$xx] . "'

现在在jQuery中这样访问,

$('.mod').on('change', function(){
    var name = $(this).val(); // 获取更改项的值
    var ctgr = $(this).attr('data-mod'); // 获取属性值
    // ....
});
英文:

Since duplicate id is not possible try using a class. And try using attribute instead another hidden input. For example,

$corpoecho .= &quot;&lt;input type=&#39;number&#39; maxlength=&#39;3&#39; class=&#39;mod&#39; data-mod=&#39;&quot;.$idriga[$xx].&quot;&#39; value=&#39;&quot;.$corpo[$xx].&quot;&#39;&gt;&quot;;

check on data-mod='".$idriga[$xx]."'

Now access in jquery like this,

$(&#39;.mod&#39;).on(&#39;change&#39;, function(){
        var name = $(this).val(); // Get the value of changed item
        var ctgr = $(this).attr(&#39;data-mod&#39;); // get arrtibute value
 ....
}

huangapple
  • 本文由 发表于 2023年3月3日 20:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定