修改从选择字段中选择的字段值

时间:2022-10-28 16:10:37

as part of my project I have two files:

作为我项目的一部分,我有两个文件:

  1. modify_brand.php (php form)
  2. modify_brand.php(php表格)
  3. update_brand.php (script to update the DB)
  4. update_brand.php(更新数据库的脚本)

In File 1. the brands are listed in a select field and displayed in a separated field that allows to modify the brand's name. The file 2. contains the query that update the DB.

在文件1中,品牌列在选择字段中,并显示在允许修改品牌名称的单独字段中。文件2.包含更新数据库的查询。

The problem consists of passing the ID of the selected brand to the UPDATE query stored in update_brand.php in order to identify the record to update.

问题在于将所选品牌的ID传递给存储在update_brand.php中的UPDATE查询,以便识别要更新的记录。

May be I am approaching the matter in a wrong way, but if not do you know how to pass to the query the ID?

可能是我以错误的方式处理此事,但如果不是,你知道如何传递查询ID吗?

modify_brand.php

modify_brand.php

<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>

<select name='brands-list'>

<?php 
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['1'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>

<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>  

<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....   
<script>
$('select[name="brands-list"]').change(function(){  
var selectedBrand = $(this).val();
$('input[name="brand-name"]').val(selectedBrand);
});
</script>

update_brand.php

update_brand.php

<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name']

if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand] );

if ($mod_brand !=''){
    $update = mysqli_query($conn,"
    UPDATE mg_terms SET name= $brand 
    WHERE term_id=......... // THIS IS WHERE THE ID OF SELECTED BRAND SHOULD BE PLACED
    ");
     header('Location: ../pages/success.html');}
     else{header('Location: ../pages/error.html');}}
       mysqli_close($conn);
    ?>

Hope I was enoughly clear.

希望我足够清楚。

1 个解决方案

#1


1  

So what you want to do is to set the select options value to the id, So when you submit your form, the selected id is submitted to the php. But you're gonna have to change your Javascript a bit. It should set the input's value to the options text, not the options value:

所以你想要做的是将select选项值设置为id,所以当你提交表单时,所选的id被提交给php。但是你将不得不改变你的Javascript。它应该将输入值设置为选项文本,而不是选项值:

<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>

<select name='brands-list'>

<?php 
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['0'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>

<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>  

<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....   
<script>
$('select[name="brands-list"]').change(function(){  
$('input[name="brand-name"]').val($('select[name="brands-list"] option:selected').text().split(' - ')[1]);
});
</script>

And on the php side:

在PHP方面:

<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name'];
$mod_id=$_POST['brands-list'];

if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand );
$brand_id=intval($mod_id );

if ($mod_brand !=''){
    $update = mysqli_query($conn,"
    UPDATE mg_terms SET name= '$brand' 
    WHERE term_id=$brand_id
    ");
     header('Location: ../pages/success.html');}
     else{header('Location: ../pages/error.html');}}
       mysqli_close($conn);
    ?>

#1


1  

So what you want to do is to set the select options value to the id, So when you submit your form, the selected id is submitted to the php. But you're gonna have to change your Javascript a bit. It should set the input's value to the options text, not the options value:

所以你想要做的是将select选项值设置为id,所以当你提交表单时,所选的id被提交给php。但是你将不得不改变你的Javascript。它应该将输入值设置为选项文本,而不是选项值:

<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>

<select name='brands-list'>

<?php 
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['0'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>

<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>  

<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....   
<script>
$('select[name="brands-list"]').change(function(){  
$('input[name="brand-name"]').val($('select[name="brands-list"] option:selected').text().split(' - ')[1]);
});
</script>

And on the php side:

在PHP方面:

<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name'];
$mod_id=$_POST['brands-list'];

if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand );
$brand_id=intval($mod_id );

if ($mod_brand !=''){
    $update = mysqli_query($conn,"
    UPDATE mg_terms SET name= '$brand' 
    WHERE term_id=$brand_id
    ");
     header('Location: ../pages/success.html');}
     else{header('Location: ../pages/error.html');}}
       mysqli_close($conn);
    ?>