dongxue163536 2017-08-17 16:51
浏览 47
已采纳

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

as part of my project I have two files:

  1. modify_brand.php (php form)
  2. update_brand.php (script to update the DB)

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.

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.

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?

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

<?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条回答 默认 最新

  • dqd2800 2017-08-17 17:38
    关注

    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:

    <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
    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);
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊