0

I am trying to save the value I get from a drop-down list to a PHP variable. I am currently able to do so but through another page. However, I want to do more than just echoing the variable, I want to use it as a parameter for one of my PHP functions on the same page.

I've tried several other solutions I found online but all of them had a submit button, which I don't want. I just want the variable to change on the same page as soon as the drop-down value is selected.
I also tried replacing 'ClientAjax.php' with my current page 'Month.php' but all I get is the same page inside the '#result' div.

Dropdown menu: (Month.php)
Used to populate my dropdown with values from a database.

<select id="clientselect">
<option selected>ALL</option>
<?php echo getClients(); ?>
</select>

<div id="result"></div>

AJAX: (Month.php)
I post twice since I need to show the default selected value as soon as I refresh.

<script>
    $(function(){
        var displayclient=$("#clientselect option:selected").text();
            $.post('ClientAjax.php',{postclient:displayclient},
            function(data){
                $("#result").html(data)
            });
        $("#clientselect").change(function (){
            var displayclient=$("#clientselect option:selected").text();
            $.post('ClientAjax.php',{postclient:displayclient},
            function(data){
                $("#result").html(data)
            });
        })
    })
</script>

ClientAjax.php

<?php 
$client = $_POST['postclient'];
?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
ZoneMeOut
  • 113
  • 3
  • 8
  • To change a variable in the current page you do not need PHP, just JavaScript. Can you tell us more about exactly what you want? – Jay Blanchard Jul 22 '19 at 18:32
  • I don't know wether it's possible or not I'm 90% sure it's not possible 10% sure that it's possible but you'll have to jump through some hoops I.e. use js . Remember php is a backend language (emphasis on the word back) – Dev Man Jul 22 '19 at 18:33
  • If you need a variable to use in PHP then just have your AJAX script post the value to $_SESSION variable. Then change the function that needs the variable to use $_SESSION instead. – CharlesEF Jul 22 '19 at 18:35
  • @CharlesEF your answer seems like the kind of answer I saw online. Could you show me an example please? – ZoneMeOut Jul 22 '19 at 18:38
  • @JayBlanchard Basically I want the user to select a value (client) from the dropdown list. Then this selected value will be used as a parameter in one of my php functions that will add the client variable to the query so the query is client specific and output the results in a google timeline. So in general I just want the value selected in the dropdown to be updated on the same page as a php var – ZoneMeOut Jul 22 '19 at 18:41
  • If you select something on the client-side you can send it server-side via AJAX and have it processed. Either that or you reload the page. – Jay Blanchard Jul 22 '19 at 18:45
  • @JayBlanchard I already know that. Check my code. Im able to send it to server side via AJAX except that its on another php page, and not the php page I want to use to call my function with the var as parameter – ZoneMeOut Jul 22 '19 at 18:50
  • You cannot do that. The PHP for the current page has already been processed. You can change variables in your current page using JavaScript, but you cannot re-process the PHP for your current page without a reload. – Jay Blanchard Jul 22 '19 at 18:51
  • If I don't understand your question it is only because you're not explaining yourself well. If you want to change a variable locally just do it with JavaScript locally. No need for a round trip to the server. – Jay Blanchard Jul 22 '19 at 18:56
  • So you're saying if I need the variable to reload in the same page using php I could use a submit button that reloads my page? Because php wont run again unless i reload? – ZoneMeOut Jul 22 '19 at 18:58
  • Right. But you could do this: https://jsfiddle.net/0pw57coh/ – Jay Blanchard Jul 22 '19 at 19:04
  • @JayBlanchard you STILL dont get my point. I DO NOT need a javascript variable. I can't do anything with that to edit my query that i need to edit in PHP. I need a PHP variable. Take time to read my code and understand what I have already established. – ZoneMeOut Jul 22 '19 at 19:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196831/discussion-between-karl-joey-g-chami-and-jay-blanchard). – ZoneMeOut Jul 22 '19 at 19:11
  • I have read your code and you still aren't clear on what you want. I have told you how to edit your PHP (you must reload) or you must use AJAX and a redirect. There are no other ways for you to accomplish what you want to do. Please do not tell me to take the time to read your code, I have read it and it is not evident from your code what you want. – Jay Blanchard Jul 22 '19 at 19:12

1 Answers1

0

Before you can use $_SESSION variables you need to start a session. Use the PHP command 'session_start()' to do this, most likely on the page that will use the $_SESSION variable. Be sure to place the command at the very top of the PHP block.

Then in your AJAX php script you need to place this command after the variable value is set. $_SESSION["var_name"] = $variable;

Then in the function that will use the variable just replace whatever variable you are using now with $_SESSION["var_name"].

This will only work if you do a submit sometime after the AJAX call. The PHP function on the page will not run again until you submit. Maybe this approach is not for you?

Post back if you need any more help, CharlesEF

CharlesEF
  • 608
  • 1
  • 15
  • 25
  • Not me, but could you please elaborate more? According to the previous comments it is not possible to change the php variable unless the page is reloaded – ZoneMeOut Jul 22 '19 at 19:02
  • Yes, that is correct. PHP is server-side and only runs when the page is loading. Once loaded Javascript takes over for client-side. You can use AJAX to change a javascript variable value, then submit that page to itself to run the PHP function. Place the AJAX variable value in to a hidden input element (be sure to give it a name). Once submitted the hidden input value will show in the $_POST array where you can now use it. – CharlesEF Jul 22 '19 at 19:12