1

I have a select option that shows the states and I have a DIV return the cost of shipping of the selected state my problem is that page refreshed when I select an option and return the value of DIV I want to display the value of DIV without refresh the whole page

    <html>
<body>
<div id ="content"> this is the cost of shipping of :
<?php 
    $ship=20;
    if (isset($_POST['city'])){
        $listings = $_POST['city'];
    } else if (isset($_GET['city'])) {
        $listings = $_GET['city'];
    } else {
        $listings = 'city';
    }
    switch ($listings) {
        case 'California':
            $ship=$ship+5; echo $ship;
            break;
        case 'Alabama':
            $ship=$ship+10; echo $ship;
            break;
        case 'Texas':
            $ship=$ship+15; echo $ship;
    }

?>
    </div>
    <form id = "form1" name = "form" >
        <select id ="city" name ="city" method="post"   onchange="this.form.submit()"    >
            <option value="California" <?php echo (isset($_POST['city']) && $_POST['city'] == 'California') ? 'selected="selected"' : ''; ?>>California</option>
            <option value="Alabama" <?php echo (isset($_POST['city']) && $_POST['city'] == 'Alabama') ? 'selected="selected"' : ''; ?>>Alabama</option>             
            <option value="Texas" <?php echo (isset($_POST['city']) && $_POST['city'] == 'Texas') ? 'selected="selected"' : ''; ?>>Texas</option>
    
        </select>
    
    </form>


</body>
</html>

java script

   <script type="text/javascript" >
$("#form1").click(function(){
    $("#content").load(" #content");
});
</script>
4
  • 1
    use ajax . did you tried it ? If yes what error are you getting ? Commented Jul 22, 2020 at 4:17
  • i used ajax but nothing shown from the script , please help me .. Commented Jul 22, 2020 at 4:19
  • 1
    add code which you have tried as well in your question Commented Jul 22, 2020 at 4:20
  • i adde a script and remove the Onchange from select option that i used but nothing change Commented Jul 22, 2020 at 4:24

1 Answer 1

2

You need to use onchange event of jquery and get value of select then pass the same to your php page using ajax . Changes you need to make in your code :

Your html code:

  <!--remove onchange from here-->
  <select id ="city" name ="city">
     <!--same code -->
   </select>

Your jquery code :

//onchange of select
$("#city").change(function() {
  //get select value
  var city = $(this).val();
   console.log(city)
  //ajax call 
  $.ajax({
     url: 'urlofphppage',
    type: 'POST',
    data: {
      'city': city
    },
     success: function(data) {
      alert('Data: ' + data);
      //add data which recieve from server to div
      $("#content").html(data);
    }

  });
});

Your php page :

 if (isset($_POST['city'])){
        $listings = $_POST['city'];
   
    switch ($listings) {
        case 'California':
            $ship=$ship+5; 
            echo $ship;//will send back to ajax as response
            break;
        case 'Alabama':
            $ship=$ship+10;
             echo $ship; //will send back to ajax as response
            break;
        case 'Texas':
            $ship=$ship+15;
           echo $ship; //will send back to ajax as response
    }
  }
Sign up to request clarification or add additional context in comments.

6 Comments

It will automcatically gets call when you change any options . You can test it change option in select and check your browser console you will see value of current option selected.Check this for more info.
it's work by adding <script src="ajax.googleapis.com/ajax/libs/jquery/3.5.1/…> but another select option shown
Can you elaborate on that? What do you mean by another select option shown?
Yes because in your code you have 2 select box and I think id of both you have given same here please change id of second select box and accordingly change $("#city")... as well.
|

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.