0

I have no experience in PHP, HTML or JavaScript and I ask for help.

I have php code that displays one select, (strings of this select is from MySQL database) and two input fields. MySQL table "g_achievement" format:

id;     achievement;        craft_1_points;     craft_2_points;  
0;      Medal Gold;         5;                  8;  
1;      Medal Silver;       10;                 15;  

select and inputs

I need to autofill inputs by data from "craft_1_points" and "craft_2_points" when I select in select field.

My code:

echo "<label for=\"achievement_id\">Achievement</label><br/>";
    $sql2 = "SELECT * FROM g_achievement";
    $result_select2 = mysql_query($sql2);
    /*drop-down list*/
    echo "<select name = 'achievement'>";
    while($object2 = mysql_fetch_object($result_select2)){
        echo "<option value = '$object2->achievement'>$object2->achievement</option>";
    }
    echo "</select>";
//============================
    echo "<label for=\"craft_1\">Craft 1</label><br/>";
    echo "<input type=\"number\" name=\"craft_1\" size=\"30\"><br/>";
    echo "<label for=\"craft_2\">Craft 2</label><br/>";
    echo "<input type=\"number\" name=\"craft_2\" size=\"30\"><br/>";

What is language I need to use? JavaScript? What is code I need?

Help me please. Thank you.

1
  • Search for php ajax cascading select Commented Feb 3, 2017 at 7:21

2 Answers 2

1

For that, you have to use Javascript in 2 ways.

  1. you can fetch all those data from table to client side then by Javascript on select of any option from drop-down list you can populate the related value in the INPUT box.
  2. On select of any option from the drop-down, you can make an Ajax call to server side & fetch the data from server & populate those in client side by Javascript.

Following is an example of the 1st way which above I've described.

<?php
    // Following is a sample data. You can replace this data by getting it from your DB
    $dataSource = array(
            0 => array(
                    'achievement' => 'Medal Gold',
                    'craft_1_points' => 5,
                    'craft_2_points' => 8,
                ),
            1 => array(
                    'achievement' => 'Medal Silver',
                    'craft_1_points' => 10,
                    'craft_2_points' => 15,
                )
        );

    $dataSourceInJson = json_encode($dataSource);
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $( document ).ready(function() {
            var dataSource = '<?php echo $dataSourceInJson; ?>';

            $( "#achievements" ).click(function() {
                var achievementSelected = $(this).val();

                $.each(JSON.parse(dataSource), function( index, value ) {
                    var achievementType = value.achievement;

                    if(achievementSelected == achievementType){
                        // Extract related Craft values from JSON object
                        var craft_1_points = value.craft_1_points;
                        var craft_2_points = value.craft_2_points;

                        // Place craft value in respective INPUT box
                        $('#craft_1_points').val(craft_1_points);
                        $('#craft_2_points').val(craft_2_points);
                    }

                });
            });
        });
    </script>
</head>
<body>
    <select id='achievements' name="achievements">
        <option value="">Select Achivements</option>
        <?php
            foreach ($dataSource as $key => $value)
            {
                echo "<option value='$value[achievement]'>$value[achievement]</option>";
            }
        ?>
    </select>

    <br>Craft 1<br>
    <input type="text" id="craft_1_points" name="craft_1_points" value="">

    <br>Craft 2<br>
    <input type="text" id="craft_2_points" name="craft_2_points" value="">
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

You are great, thank you very much!) Only I use this code in Wordpress and I was needed to replace $ by jQuery.
Glad to know it's helped you.
0
set dbConn=Server.CreateObject("ADODB.Connection")
dbConn.Open strProvider
rowCount=0
sqlTable = "select id, name from [sysobjects] where type in ('U','V','P') and category<>2 Order By Name "
set rsSchema=dbConn.execute(sqlTable)
%>
<select  name="getTable" class="selectpicker form-control show-tick" data-live-search="true">
<%
do until rsSchema.EOF %>
 <option value='<%=rsSchema(0)%>'><%= rsSchema(1)%>
<% rsSchema.MoveNext
loop
rsSchema.Close
set rsSchema = Nothing %></option>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.