简体   繁体   English

在同一页面中使用Java脚本/ ajax将值从html选择字段传递给mysql查询,而无需刷新页面

[英]Passing a value from html select field to mysql query using java script/ajax in same page with out refreshing the page

I have the following select field, values are populating from mysql table. 我有以下选择字段,值从mysql表填充。 The dropdown contains around 8-10 values. 下拉列表包含约8-10个值。

<select id ="name" name="name" class="form" required>
    <option value="" disabled selected>Name</option>
    <?php
    $sql = "select name  from accounts where id = '{$_SESSION['SESS_MEMBER_ID']}'";
    $result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
    if (mysqli_num_rows($result) > 0)
        while ($row = mysqli_fetch_array($result)) {
            $name = ($row['name']);
            echo '<option value=".$name.">' . $name . '</option>';
        }
    ?>
</select>

How can I pass the selected value from the above dropdown list to the below sql query(to where condition)? 我怎样才能从上面的下拉列表中选择的值传递到下面的SQL查询(到哪里条件)? And also I need to populate the output of the below query to the below two input fields. 我还需要将以下查询的输出填充到以下两个输入字段。 Both the above select field and below input fields are in same page. 上面的选择字段和下面的输入字段都在同一页面中。

<?php
$sql = "select income,expense from accounts where name = ''  and id = '{$_SESSION['SESS_MEMBER_ID']}'";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
if (mysqli_num_rows($result) > 0)
    while ($row = mysqli_fetch_array($result)) {
        $inc = $row['income'];
        $exp = $row['expense'];
        ?>
<div class="col-md-3">                                                                                                                                                        
            <input value="<?php echo $inc ?>" class="form" readonly/>
        </div>

        <div class="col-md-3 ">                                                                                                                                                        
            <input value="<?php echo $exp ?>" class="form" readonly/>
</div>

Thanks in advance for suggessions and advice. 在此先感谢您的建议和建议。

Using Jquery you can do an on change function: 使用Jquery,您可以执行on change函数:

$('#name').change(function (e) {
    var parameter = $(this).val();

    Pass Parameter to your query and return values
    $('#input1ID').val($row['income']);
    $('#input2ID').val($row['expense']);
})

Sorry I don't know PHP or how to start the function, but this will pass a value to the query everytime you change the dropdown. 抱歉,我不知道PHP或如何启动该函数,但是每次您更改下拉列表时,这都会将值传递给查询。 This will not refresh the page. 这不会刷新页面。

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

相关问题 在Spring MVC中使用jQuery AJAX从HTML页面传递值 - passing a value from a html page using jQuery ajax in spring mvc 在同一页面上传递查询AJAX - passing query on the same page AJAX 使用ajax在同一页面中传递值 - passing value in same page with ajax 我想在不刷新页面的情况下使用从 ajax 帖子返回的值更新 html 表单中数字字段的最小值 - I want to update a min value of a number field in a html form with a value returned from an ajax post without refreshing the page 查询SQL而不使用Ajax刷新页面 - Query SQL without refreshing the page using Ajax 如何在同一页面上使用windows.location.href将js值传递给php时停止刷新页面 - How to stop refreshing page when passing js value to php using windows.location.href on the same page 在不刷新页面php的情况下获取同一页面中的输入字段值 - Get input field value in same page without refreshing page php 使用javascript将值从php页面传递到html页面 - passing value from a php page to html page using javascript 如何在不刷新HTML页面的情况下在表上显示数据(使用Node.js和MySQL Ajax) - How to Display data on a table without refreshing html page (using nodejs & mysql ajax) 将选择值传递给控制器​​,而无需刷新页面(AJAX,RAILS,JS) - Pass select value to controller without refreshing the page (AJAX, RAILS, JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM