如何从包含数组的输入类型的名称中调用值$_POST[‘variable+array’]?

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

how to call value $_POST['variable+array'] from name that contain array in input type

问题

I am a newbie, I have a problem with this code, please help.

<input type="text" id="hargautama<?php echo $row['idharga']; ?>" name="hargautama<?php echo $row['idharga']; ?>">

My problem is on this

if(isset($_POST['updatehargautama'])){
	
	$hargautama = $_POST['????'];

I want to insert this value into the database from this input type

<input type="text" id="hargautama<?php echo $row['idharga']; ?>" name="hargautama<?php echo $row['idharga']; ?>">

Thanks for the help, guys. I owe you all.

英文:

iam a newbie, i have a problem with this code, please help.

&lt;input type=&quot;text&quot; id=&quot;hargautama&lt;?php echo $row[&#39;idharga&#39;]; ?&gt;&quot; name=&quot;hargautama&lt;?php echo $row[&#39;idharga&#39;]; ?&gt;&quot;&gt;

my problem is on this

if(isset($_POST[&#39;updatehargautama&#39;])){
	
	$hargautama = $_POST[&#39;????&#39;];

i want to insert this value to database from this input type

&lt;input type=&quot;text&quot; id=&quot;hargautama&lt;?php echo $row[&#39;idharga&#39;]; ?&gt;&quot; name=&quot;hargautama&lt;?php echo $row[&#39;idharga&#39;]; ?&gt;&quot;&gt;

thanks for the help guys. i owe u all.

答案1

得分: 1

假设表单提交到相同的页面,您可以像这样修改您的代码:

<?php
if (isset($_POST['updatehargautama'])) {
    $idharga = $_POST['idharga'];
    $hargautama = $_POST['hargautama' . $idharga];

    // 现在您已经获得了名称为'hargautama<idharga>'的输入字段的值
}
?>

<!-- 您的HTML表单 -->
<form method="post" action="">
    <input type="hidden" name="idharga" value="<?php echo $row['idharga']; ?>">
    <input type="text" id="hargautama<?php echo $row['idharga']; ?>" name="hargautama<?php echo $row['idharga']; ?>">
    <input type="submit" name="updatehargautama" value="Update">
</form>

在这段代码中,我添加了一个名为"idharga"的隐藏输入字段,用于存储$row['idharga']的值。当表单提交时,它将同时发送名称为'hargautama'的输入字段的值以及$_POST数组中的'idharga'值。然后,访问'idharga'值以构建正确的输入字段名称并检索相应的值。

英文:

Assuming the form is submitted to the same page, you can modify your code like this:

&lt;?php
if (isset($_POST[&#39;updatehargautama&#39;])) {
    $idharga = $_POST[&#39;idharga&#39;];
    $hargautama = $_POST[&#39;hargautama&#39;.$idharga];

    // Now you have the value from the input field with the name &#39;hargautama&lt;idharga&gt;&#39;
}?&gt;

//Your HTML form
&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;idharga&quot; value=&quot;&lt;?php echo $row[&#39;idharga&#39;]; ?&gt;&quot;&gt;
    &lt;input type=&quot;text&quot; id=&quot;hargautama&lt;?php echo $row[&#39;idharga&#39;]; ?&gt;&quot; name=&quot;hargautama&lt;?php echo $row[&#39;idharga&#39;]; ?&gt;&quot;&gt;
    &lt;input type=&quot;submit&quot; name=&quot;updatehargautama&quot; value=&quot;Update&quot;&gt;
&lt;/form&gt;

In this code, I've added a hidden input field named "idharga" to store the value of $row['idharga']. When the form is submitted, it will send both the value from the input field with the name 'hargautama<idharga>' and the 'idharga' value in the $_POST array. then access the 'idharga' value to construct the correct name of the input field and retrieve the corresponding value.

答案2

得分: -1

if (isset($_POST['updatehargautama'])) 
{
    $idharga = $_POST['updatehargautama']; 
    $hargautama = $_POST['hargautama' . $idharga];
}
英文:
if (isset($_POST[&#39;updatehargautama&#39;])) 
{
       $idharga = $_POST[&#39;updatehargautama&#39;]; 
       $hargautama = $_POST[&#39;hargautama&#39; . $idharga];
}

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

发表评论

匿名网友

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

确定