如何添加下拉列表选择的值到数据库?

时间:2021-11-20 04:08:53

hello i am new to PHP and ajax. upon choosing the drop down list separate div function will appear and i have to insert both the values . i mean selected option and also the input data in separate div .sorry my english is not good. please help me friends . thank you in advance

你好,我是PHP和ajax的新手。在选择下拉列表时,将出现单独的div函数,我必须同时插入这两个值。我的意思是选择选项和独立的div的输入数据。对不起,我的英语不好。请帮助我的朋友。提前谢谢你

<p><label>Affiliate Type</label></p>
<p><select class="inp authInput" id="id_affiliate_type" name="affiliate_type">
<option value="" selected="selected" disabled="disabled" style="display: none;">Select</option>
<option value="I">Individual</option>
<option value="O">Organisation</option>
<div id="errorselect"></div>
</select></p>
<b button type="submit" class="btn btn-primary" onclick="return sendaccountData();" id="updateaccount" name="updateaccount">Update Account Information</b></button>

how to store this in phpmyadmin database using ajax and php..

如何使用ajax和php将其存储到phpmyadmin数据库中。

3 个解决方案

#1


1  

You need to implement a javascript listener to react when an option is selected. Then in this listener, you need to send an ajax request to your server with the selected option value as a post/get variable. On your server you need to implement the corresponding php file to handle the ajax request. This php file must get the post/get variable (the selected option value) and insert it in your SQL database. This is the general idea.

您需要实现一个javascript监听器,以便在选择一个选项时进行响应。然后,在这个侦听器中,您需要将一个ajax请求发送到您的服务器,并将所选的选项值作为post/get变量。在服务器上,您需要实现相应的php文件来处理ajax请求。这个php文件必须获取post/get变量(选中的选项值)并将其插入到SQL数据库中。这是总的想法。

Assuming you are using jQuery, the listener must be in a javascript file in your webpage and should look like :

假设您正在使用jQuery,那么侦听器必须位于您的网页中的javascript文件中,并且应该如下所示:

// react when the select list changes
$('#id_affiliate_type').on('change', function(e) {

    // get the selected option value
    var selected = $(this).find(':selected').text(); // or val(), need to confirm this...

    // send the ajax request to your server
    $.ajax({

        // url of your php script which insert data into database
        url: 'your_php_file.php',

        type: 'get' // or 'post' if you prefer

        // variables passed to the script
        data: {selected : selected},

        // callback, request is successful
        success: function(result) {
            console.log('success ' + result);
            // do whatever you need to do after the call
        }

        // callback, something wrong happened
        error: function(result) {
             console.log('error ' + result);
             // do whatever you need to do after the call
        }

        // process completed (success or error)
        complete: function(result) {
        }
    }
}

In your PHP file (your_php_file.php) you can access the 'selected' variable by calling :

在PHP文件(your_php_file.php)中,可以通过调用:

$selected = $_GET['selected'] // (or $_POST)

...and then use it to modify your database as needed.

…然后根据需要使用它来修改数据库。

Again this is the general idea, code above is just the general design, not to be used 'as is' but as an inspiration.

这也是一般的想法,上面的代码只是一般的设计,不像“as is”那样使用,而是作为一种灵感。

You can find lots of examples on SO and elsewhere about implementing a on('change') listener on a select list, sending an ajax request to your server with variables and update a database in PHP from these variables.

您可以在SO和其他地方找到许多关于在选择列表上实现on('change')侦听器的示例,向服务器发送一个ajax请求,并从这些变量中更新PHP数据库。

Good luck

祝你好运

#2


1  

file1.php

file1.php

<html>
<select class="opt">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
</select>
<div id="msg">
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(".opt").change(function(){
    var data1 = $(".opt").val();
    $.ajax({
        type:"GET",
        cache:false,
        url:"file2.php",
        data : { foo:data1},    // multiple data sent using ajax
        success: function (html) {

          $('#msg').html(html);
        }
      });
});
</script>
</html>

file2.php

php

<?php
$data=$_GET['foo'];
if($data=="option1")
{
    echo "selected value is option1";    // add sql operation here if option 1
}
else if($data=="option2")
{
    echo "selected value is option2";  // add sql operation here if option2
}
?>

you can display the data according to the drop down selection in file2.php,

可以根据file2.php中的下拉选项显示数据,

#3


0  

It is very simple. you can make an ajax call and get the value when call is successful and insert that to that DIV.

它是非常简单的。您可以进行ajax调用,并在调用成功时获取值,并将其插入到该DIV中。

You will require 1 php file, just for checking the database and giving output, for example: select_org_type.php

您将需要一个php文件,仅用于检查数据库并给出输出,例如:select_org_type.php

Example : select_org_type.php

例如:select_org_type.php

<?php
if(!empty($_GET['affiliate_type'])){
    //use mysqli commands to connect to database and fetch result and store in $result.
    header();//This is very important, it will set your output as pure JSON object and javascript will find it easy to integrate
    echo json_encode($result);
}

Now, here in your page, for example you are using jQuery (which I suggest would be easy to implement ajax call)

现在,在您的页面中,例如,您正在使用jQuery(我建议使用jQuery很容易实现ajax调用)

<script type="text/javascript">
    var affiliate_type = $('#id_affiliate_type').val();
    $('#updateaccount').on('click',function(){
        $.get('select_org_type.php',{affiliate_type:affiliate_type},function(data){
            $('#errorselect').text(data['column-name']);
        });
    });
</script>

This is how I make ajax call to populate any select option or give result to any div.

这就是我如何使ajax调用填充任何select选项或将结果填充到任何div的方式。

#1


1  

You need to implement a javascript listener to react when an option is selected. Then in this listener, you need to send an ajax request to your server with the selected option value as a post/get variable. On your server you need to implement the corresponding php file to handle the ajax request. This php file must get the post/get variable (the selected option value) and insert it in your SQL database. This is the general idea.

您需要实现一个javascript监听器,以便在选择一个选项时进行响应。然后,在这个侦听器中,您需要将一个ajax请求发送到您的服务器,并将所选的选项值作为post/get变量。在服务器上,您需要实现相应的php文件来处理ajax请求。这个php文件必须获取post/get变量(选中的选项值)并将其插入到SQL数据库中。这是总的想法。

Assuming you are using jQuery, the listener must be in a javascript file in your webpage and should look like :

假设您正在使用jQuery,那么侦听器必须位于您的网页中的javascript文件中,并且应该如下所示:

// react when the select list changes
$('#id_affiliate_type').on('change', function(e) {

    // get the selected option value
    var selected = $(this).find(':selected').text(); // or val(), need to confirm this...

    // send the ajax request to your server
    $.ajax({

        // url of your php script which insert data into database
        url: 'your_php_file.php',

        type: 'get' // or 'post' if you prefer

        // variables passed to the script
        data: {selected : selected},

        // callback, request is successful
        success: function(result) {
            console.log('success ' + result);
            // do whatever you need to do after the call
        }

        // callback, something wrong happened
        error: function(result) {
             console.log('error ' + result);
             // do whatever you need to do after the call
        }

        // process completed (success or error)
        complete: function(result) {
        }
    }
}

In your PHP file (your_php_file.php) you can access the 'selected' variable by calling :

在PHP文件(your_php_file.php)中,可以通过调用:

$selected = $_GET['selected'] // (or $_POST)

...and then use it to modify your database as needed.

…然后根据需要使用它来修改数据库。

Again this is the general idea, code above is just the general design, not to be used 'as is' but as an inspiration.

这也是一般的想法,上面的代码只是一般的设计,不像“as is”那样使用,而是作为一种灵感。

You can find lots of examples on SO and elsewhere about implementing a on('change') listener on a select list, sending an ajax request to your server with variables and update a database in PHP from these variables.

您可以在SO和其他地方找到许多关于在选择列表上实现on('change')侦听器的示例,向服务器发送一个ajax请求,并从这些变量中更新PHP数据库。

Good luck

祝你好运

#2


1  

file1.php

file1.php

<html>
<select class="opt">
    <option value="option1">option1</option>
    <option value="option2">option2</option>
</select>
<div id="msg">
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(".opt").change(function(){
    var data1 = $(".opt").val();
    $.ajax({
        type:"GET",
        cache:false,
        url:"file2.php",
        data : { foo:data1},    // multiple data sent using ajax
        success: function (html) {

          $('#msg').html(html);
        }
      });
});
</script>
</html>

file2.php

php

<?php
$data=$_GET['foo'];
if($data=="option1")
{
    echo "selected value is option1";    // add sql operation here if option 1
}
else if($data=="option2")
{
    echo "selected value is option2";  // add sql operation here if option2
}
?>

you can display the data according to the drop down selection in file2.php,

可以根据file2.php中的下拉选项显示数据,

#3


0  

It is very simple. you can make an ajax call and get the value when call is successful and insert that to that DIV.

它是非常简单的。您可以进行ajax调用,并在调用成功时获取值,并将其插入到该DIV中。

You will require 1 php file, just for checking the database and giving output, for example: select_org_type.php

您将需要一个php文件,仅用于检查数据库并给出输出,例如:select_org_type.php

Example : select_org_type.php

例如:select_org_type.php

<?php
if(!empty($_GET['affiliate_type'])){
    //use mysqli commands to connect to database and fetch result and store in $result.
    header();//This is very important, it will set your output as pure JSON object and javascript will find it easy to integrate
    echo json_encode($result);
}

Now, here in your page, for example you are using jQuery (which I suggest would be easy to implement ajax call)

现在,在您的页面中,例如,您正在使用jQuery(我建议使用jQuery很容易实现ajax调用)

<script type="text/javascript">
    var affiliate_type = $('#id_affiliate_type').val();
    $('#updateaccount').on('click',function(){
        $.get('select_org_type.php',{affiliate_type:affiliate_type},function(data){
            $('#errorselect').text(data['column-name']);
        });
    });
</script>

This is how I make ajax call to populate any select option or give result to any div.

这就是我如何使ajax调用填充任何select选项或将结果填充到任何div的方式。