简体   繁体   English

在mysql上更新echo“选择选项”

[英]update echo “select option ” on mysql

I have a form that updates the adds to the table. 我有一个更新添加到表中的表格。 This table is retrieved live. 该表是实时检索的。 which means once submitting the form you can view the content on the table. 这意味着一旦提交表格,您就可以查看表格中的内容。 I added a select option drop down on the table content. 我在表格内容上添加了一个选择选项下拉菜单。 I am not sure how to get this info in back to mysql once the user chooses his option. 我不确定一旦用户选择了自己的选项,如何将这些信息返回给mysql。 As seen in the code my file is a PHP file. 如代码所示,我的文件是一个PHP文件。

my select option is updates by javascript but it does not update mysql. 我的选择选项是通过javascript更新,但不更新mysql。

Kindly guide me. 请指导我。 Thank you 谢谢

          while($row = mysqli_fetch_assoc($resultset)) 

{ {

        echo "<tr id=\"row\">
           <td class=\"fname\" data-id1=".$row["contact_id"]." contenteditable>" . $row['fname'] ." </td>
           <td class=\"lname\" data-id2=".$row["contact_id"]." contenteditable>" . $row['lname']. "</td>
           <td>" . $row['contact']."</td>
           <td>
              <select id4=".$row['contact_id']." name=\"rsvp\" onchange = \"fetch_select(this.value)\">
              <option name=\"not-done\" value=\"not-done\">Not done</option>
              <option name=\"attending\" value=\"attending\">Attending</option>
              <option name=\"not-attending\" value=\"not-attending\">Not-attending</option>
              <option name=\"maybe\" value=\"maybe\" selected>Maybe</option>
              </select>
            </td>
           <td><button id3=".$row['contact_id']." name=\"button\"><i class=\"fa fa-trash-o\" aria-hidden=\"true\"></i></button>
             <tr>";

}
        echo "</table>";
        mysqli_close($con);
 ?> 







$('select').on('change', function()
{
  // Show an alert
  alert( "The new value of the select is " + $(this).val() );
  var id=$(this).attr("id4");
  var val=$(this).val();

  function fetch_select(val ,id) 
   {  
       $.ajax({  
            url:"updateselect.php",  
            method:"POST",  
            data:{rsvp:val , id:id},  
            dataType:"text",  
            success:function(data){  
                 alert(data);  
            }  
       });  
  }
});



    <?php  
 $connect = mysqli_connect("localhost", "root", "", "registration");  
 $id = $_POST["id"];  
 $rsvp = $_POST["val"];  

 $sql = "UPDATE guestlist SET rsvp ='".$rsvp."' WHERE contact_id='".$id."'";  
 if(mysqli_query($connect, $sql))  
 {  
      echo 'Data Updated';  
 }  
 ?>

You want to create a form surrounding your dropbox; 您想在保管箱周围创建一个表单; now you can either submit it using ajax or just submit it to a different page, your call. 现在您可以使用ajax提交它,也可以将其提交到另一个页面,即您的呼叫。

It would look something like this: 它看起来像这样:

Note: Add a name param to your select so you can actually get it using $_POST and handle the data. 注意:将name参数添加到您的选择中,以便您实际上可以使用$_POST来获取它并处理数据。

<form method="post" action="rsvp_handler.php">
    <?php
    while($row = mysqli_fetch_assoc($resultset)) 
    { 

        echo "<tr id=\"row\">
        <td class=\"fname\" data-id1=".$row["contact_id"]." contenteditable>" . $row['fname'] ." </td>
        <td class=\"lname\" data-id2=".$row["contact_id"]." contenteditable>" . $row['lname']. "</td>
        <td>" . $row['contact']."</td>
        <td>
          <select name='rsvp' id='rsvp'>
              <option value=\"not-done\">Not done</option>
              <option value=\"attending\">Attending</option>
              <option value=\"not-attending\">Not-attending</option>
              <option value=\"maybe\" selected>Maybe</option>
          </select>
        </td>
        <td><button id3=".$row['contact_id']." name=\"button\"><i class=\"fa fa-trash-o\" aria-hidden=\"true\"></i></button>
   <tr>";

}
        echo "</table>";
        mysqli_close($con);
   ?> 
    <button type="submit" name="submit">Submit</button>
</form>

Then for the rsvp_handler.php page just take the post result and then send the wanted data to your database: 然后对于rsvp_handler.php页面,只需获取发布结果,然后将所需数据发送到您的数据库:

if (isset($_POST['submit']))
{
    $rsvp = $_POST['rsvp'];
    //... any other elements

    // create your query here
    $sql = "INSERT INTO " . $table . " (rsvp, row2, row3) VALUES (?, ?, ?)";
    // best way is to create prepared statements
    if($stmt = $mysqli->prepare( $sql ))
    {
        // bind the params
        $stmt->bind_param('sss', $rsvp, $val2, $val3);
        // execute the query
        $stmt->execute();
        // close the connection
        $stmt->close();
    }
}

Hi I found the answer to the solutions. 嗨,我找到了解决方案的答案。

My ajax need to have a separate function. 我的ajax需要具有单独的功能。

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

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