Category: AJAX

How to update session variable from dropdown change?

If you want to update your value like when drop down change then this value will set in php session in this case you can use ajax which help you lot. This is simple code which update session value when change drop down change .

This is total html and php code which need to update and browse data

<?php
session_start(); // for show your session value 
print_r($_SESSION); // remove this after check
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form name="myForm" action="/thanks" method="POST">

    <table>
        <tr>
            <td style="width:272px;">
                <select name="select_amount" id="select_amount" onchange="update_session_value(this.value)">
                    <option value="0">select a listing type</option>
                    <option value="10">Premium Listings</option>
                    <option value="20">Premium Blogs</option>
                    <option value="30">1 week sticky</option>
                </select></td>

            <td style="text-align:left;color:gray;">
                <span style="color:crimson;">*
            </span>Select a listing type
            </td>
        </tr>
    </table>
    <input type="submit" name="submit" class="button_add" onsubmit="return validateForm()">

</form>

<script>
    function update_session_value(value) {
        $.ajax({
            type: "POST",
            url: 'http://localhost/session.php', // change url as your 
            data: 'select_amount=' + value,
            dataType: 'json',
            success: function (data) {

            }
        });
    }

</script>

Then create php file which update session value

<?php
session_start();
if( isset($_POST['select_amount']) ) {
    // save values from other page to session
    $_SESSION['amount'] = $_POST['select_amount'];

}
?>

For test you can refresh the page and check output or ajax function update this code

<script>
    function update_session_value(value) {
        $.ajax({
            type: "POST",
            url: 'http://localhost/session.php', // change url as your 
            data: 'select_amount=' + value,
            dataType: 'json',
            success: function (data) {
 window.location.reload;
            }
        });
    }

</script>

if you face any problem then please inform me, I will try to help you

Comment

Sending data using Ajax to PHP

When you need to send data to server side then you can use Ajax to send data. This is simple code which help you.


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>PHP, jQuery search demo</title>
    <link rel="stylesheet" type="text/css" href="my.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("input").keyup(function () {
                $('#results').html('');
                var searchString = $("#search_box").val();
                var data = 'search_text=' + searchString;
                if (searchString) {
                    $.ajax({
                        type: "POST",
                        url: 'http://localhost/search.php',
                        data: data,
                        dataType: 'text',
                        async: false,
                        cache: false,
                        success: function (result) {
                            $('#results').html(result);
                            //window.location.reload();

                        }
                    });
                }
            });
        });
    </script>

</head>
<body>
<div id="container">
    <div style="margin:20px auto; text-align: center;">
        <form method="post" action="do_search.php">
            <input type="text" name="search" id="search_box" class='search_box'/>
            <input type="submit" value="Search" class="search_button"/><br/>
        </form>
    </div>
    <div>

        <div id="searchresults">Search results :</div>
        <ul id="results" class="update">
        </ul>

    </div>
</div>

</body>
</html>

then create a search.php file which process ajax request value. you can also select data from database using this ajax data, also set this data to session etc.

This is search.php file

<?php 

$searchquery = $_POST['search_text']; 
echo $searchquery;
?>

if you face any problem then please inform me.

Comment