-2

Can anyone help me out to convert the PHP code to Ajax?

HTML Code:

  <form method="POST" >
    <label for="Manufacturer"> Manufacturer : </label>
      <select id="cmbMake" name="Make"     onchange="document.getElementById('selected_text').value=this.options[this.selectedIndex].text">
         <option value="0">Select Manufacturer</option>
         <option value="1">--Any--</option>
         <option value="2">Toyota</option>
         <option value="3">Nissan</option>
    </select>
    <input type="hidden" name="selected_text" id="selected_text" value="" />
    <input type="submit" name="search" value="Search"/>
    </form>

PHP Code:

 <?php

if(isset($_POST['search']))
{
    $makerValue = $_POST['Make']; // make value
    $maker = $_POST['selected_text']; // get the selected text
    echo $maker;
}
 ?>

Source:

  1. PHP code to get selected text of a combo box
return
  • 1,049
  • 5
  • 17
  • 27
  • 3
    I don't see any code that shows you tried AJAX! – Akam Jan 21 '20 at 06:12
  • @Akam, I meant convert my code to Ajax. – return Jan 21 '20 at 06:24
  • You're not asking us to "convert" anything. You're asking us to write your code for you. Please make some research and attempts before asking here. SO isn't a coding service. – M. Eriksson Jan 21 '20 at 06:25
  • @magnusEriksson, I dont know much about Ajax, thats why i asked for help. i have already tried a lot but didnt got any result. Thanks – return Jan 21 '20 at 06:28
  • Google "Get started with ajax" and you will find _many many_ tutorials. You should always do extensive research and make some proper attempts yourself before asking. Then, if you run into some _specific_ issue with your code while trying, we can help from there. – M. Eriksson Jan 21 '20 at 06:28
  • @magnusEriksson, As I have said I have tried with a lot of code that I have found in examples in the Google, but I couldn't do it myself and even didn't know which code is good for my purpose. Thanks – return Jan 21 '20 at 06:36
  • And like I said, we can help you sort out issues with your code (which means that you need to post your attempt and explain what happens). We are _not_ here to write your code for you. Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – M. Eriksson Jan 21 '20 at 06:37

1 Answers1

0

You have to trigger the button submit and prevent it to reload the page, see code below

 $('#btn-search').click(function(e) {
      e.preventDefault()
       $.ajax({
         url:"url to your search php function",
         method:"POST",
         data:{
           selected_text: $('#selected_text').val(),
           Make: $('#cmbMake').val()
         },
         success:function(data)
         {
             //success code here
         }
    });
 })

put an ID to you submit button

in your php, just remove your if condition and make a function like this one

public function search() {
    $makerValue = $_POST['Make']; // make value
    $maker = $_POST['selected_text']; // get the selected text
    echo $maker;
}
Qonvex620
  • 3,819
  • 1
  • 8
  • 15