Skip to content Skip to sidebar Skip to footer

Back To Php From Jquery

I want to echo the HTML dropdown value through jQuery: <

Solution 1:

You can't access select values in PHP without first submitting the form.

Here's a sample form which submits to a file called submit.php

<formmethod="post"action="submit.php"><selectid="my_select"name="my_select"><optionvalue="1">One</select><optionvalue="2">Two</select><optionvalue="3">Three</select></select></form>

The contents of submit.php would look like this...

<?phpecho$_POST['my_select']; // Will output the value of my_select when the form was submitted?>

You could also use ajax to submit data from your form to a PHP page, have the PHP page generate some HTML output and return it back to your jQuery, from there you can insert the resulting HTML into an element (div) in your page.

<formid="my_form"method="post"action="submit.php"><label>Select the number of items you'd like @ £5.00 each</label><br><selectname="my_select"><optionvalue="1">One</select><optionvalue="2">Two</select><optionvalue="3">Three</select></select></form><divid="result"></div>

Create a PHP page to receive the data from the form.

$price = 5.00;
$noOfItems = $_POST['my_select'];

$totalCost = $price * $noOfItems;

echo"<p>$noOfItems @ $price will cost you $totalCost"; 

Then use some jQuery to submit the data from your form to the PHP back in the background, and then put the resulting HTML into the #result div.

<scripttype="text/javascript">
$(document.ready(function(){

    $("#my_select").change(function(){
        $.post("submit.php", $("#my_form").serialize(), function(response){
            $("#result").html(response);
        });
    });

}))
</script>

Post a Comment for "Back To Php From Jquery"