michaelfurey Posted May 4, 2017 Share Posted May 4, 2017 I would like to change the <p> text in the following way but it doesn't work: PHP code: echo "<p id=\"change_me\">" . $_POST['data'] . "</p>"; echo "<select onchange=\"getval(this);\" id=\"status\" multiple>"; echo "<option value=\"1\">One</option>"; echo "<option value=\"2\">Two</option>"; echo "<option value=\"3\">One</option>"; echo "<option value=\"4\">Two</option>"; echo "</select>"; JS code: $(function() { // on page load $('#status').on("change",function(e) { $("#change_me").empty(); // clear the container var selectedOptionValue = $(this).val(); if (selectedOptionValue) { // in case they select "Please select" $.ajax({ url: '/other/shipments.php', type: 'POST', data: {data : selectedOptionValue }, dataType: 'text', success: function(data){ $("#change_me").html(data); } }); } }); }); I would like to echo the selected option values of the multiple select but it doesn't work. For example if I substitute $("#change_me").html(data); with $("#change_me").html('viva'); PHP echo correctly the string 'viva'... but I would like to echo the selected values 'data' and the code doesn't work. Can someone help me? Quote Link to comment https://forums.phpfreaks.com/topic/303866-change-text-with-ajax-and-php/ Share on other sites More sharing options...
Jacques1 Posted May 4, 2017 Share Posted May 4, 2017 The obvious question: What exactly is data? console.log(data); Note that you cannot put arbitrary content into p elements. Quote Link to comment https://forums.phpfreaks.com/topic/303866-change-text-with-ajax-and-php/#findComment-1546197 Share on other sites More sharing options...
michaelfurey Posted May 5, 2017 Author Share Posted May 5, 2017 The obvious question: What exactly is data? console.log(data); Note that you cannot put arbitrary content into p elements. data is a way that I tried to put inside the <p> element the selected option values with ajax... I would like to echo 'selectedOptionValue' inside the <p> element. So if I select the option One and Two I would like to read inside the <p> One and Two. Quote Link to comment https://forums.phpfreaks.com/topic/303866-change-text-with-ajax-and-php/#findComment-1546212 Share on other sites More sharing options...
Jacques1 Posted May 5, 2017 Share Posted May 5, 2017 Then what's the whole point of reading the response from the server? You already have the selected option. $("#change_me").text(selectedOptionValue); Quote Link to comment https://forums.phpfreaks.com/topic/303866-change-text-with-ajax-and-php/#findComment-1546215 Share on other sites More sharing options...
michaelfurey Posted May 5, 2017 Author Share Posted May 5, 2017 Then what's the whole point of reading the response from the server? You already have the selected option. $("#change_me").text(selectedOptionValue); Because I am using a particular web framework written in PHP and to apply a change I have to reload the entire page.... so I was thinking to use Ajax in order to reload only an element to apply a change, as a <p> in this case. Quote Link to comment https://forums.phpfreaks.com/topic/303866-change-text-with-ajax-and-php/#findComment-1546223 Share on other sites More sharing options...
Jacques1 Posted May 5, 2017 Share Posted May 5, 2017 Again: You don't need the server to tell you which option the user has selected. The browser already knows that, because this has happened in the browser. So if all you want to do is put the selected value into the paragraph, then just use selectedOptionValue as I've shown you The server response is irrelevant. I'm not sure why this is so hard to understand when the code is right in front of you. Quote Link to comment https://forums.phpfreaks.com/topic/303866-change-text-with-ajax-and-php/#findComment-1546226 Share on other sites More sharing options...
Jacques1 Posted May 5, 2017 Share Posted May 5, 2017 If you're struggling with the fact that you have multiple values, just use join() $("#change_me").text(selectedOptionValue.join(" and ")); // selectedOptionValue*s* might be a better name Quote Link to comment https://forums.phpfreaks.com/topic/303866-change-text-with-ajax-and-php/#findComment-1546233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.