0

My form is like the one above:

<form id="calculator">
<input type="text" name="height" id="height"/>
<input type="text" name="inferiorPanel" id="inferiorPanel" readonly />
<button type="button" onclick="calculatePanels();">
</form>

Jquery script is the next one:

function calculatePanels() {
     var Height = document.forms["calculator"]["height"].value;

     $.ajax({
                url: "includes/inferiorPanel.php",
                success: function(result){
            $("#inferiorPanel").val(result);
            }});
}

And PHP code from inferiorPanel.php is:

<?php
$height = ????; 

        switch ($height) {

            case ($height > 0 && $height <= 1845):
            echo 615;
            break;

            case ($height > 1845 && $height <= 1980):
            echo 495;
            break;

            case ($height > 1980 && $height <= 2100):
            echo 495;
            break;

            case ($height > 2100 && $height <= 2220):
            echo 495;
            break;

            case ($height > 2220 && $height <= 2340):
            echo 615;
            break;

            case ($height > 2340 && $height <= 2460):
            echo 615;
            break;

            case ($height > 2460 && $height <= 2475):
            echo 495;
            break;

            case ($height > 2475 && $height <= 2595):
            echo 615;
            break;

            case ($height > 2595 && $height <= 2715):
            echo 615;
            break;

            case ($height > 2715 && $height <= 2835):
            echo 615;
            break;

            case ($height > 2835 && $height <= 2955):
            echo 615;
            break;

            case ($height > 2955 && $height <= 3000):
            echo 615;
            break;
}

My question is: How to pass the variable height in PHP without using POST on Ajax, in order to show the result in input inferiorPanel?

I do not want to use <input type="submit" /> because will refresh the page

3 Answers 3

1

Page refreshes not because you use $_POST, but because your code is incomplete.

Here what you need to do (you have jquery, so use it):

  1. Remove onclick from button, remove <button>, add submit:

    <form id="calculator">
        <input type="text" name="height" id="height"/>
        <input type="text" name="inferiorPanel" id="inferiorPanel" readonly />
        <input type="submit" name="Name" value="Value">
    </form>
    
  2. Bind a handler to submit event of your form

    $( "#calculator" ).submit(function() {
        var Height = $( "#height" ).val();
    
        $.ajax({
            url: "includes/inferiorPanel.php",
            data: { height: Height},
            type: "POST",
            success: function(result){
                $("#inferiorPanel").val(result);
            }
        });
    
        // simple way to prevent default action of form submission:
        return false;
    } );
    
  3. On server side:

    <?php
    $height = $_POST['height']; 
    switch ($height) {
        // run your switch here
    }
    ?>
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. It worked perfectly with submit thanks to return false. Thanks again.
0

you can use POST without refreshing the page

 $.ajax({
     url: "includes/inferiorPanel.php",
     method: "POST",
     data : {'height' : Height},
     success: function(result){
         $("#inferiorPanel").val(result);
     }
 });

and in php :

<?php
    $height = $_POST['height'];
?>

1 Comment

Thanks for the answer.
0

You can specify GET in the Ajax call and have height in the URL as follows

var height = $('#height').val();


 $.ajax({
        type        : 'GET', 
        url         : 'inferiorPanel.php?height=' + height,

Within your php you can just say if(isset($_GET['height']) $height = $_GET['height'];

2 Comments

Thanks. I went on POST although, with return false in order not to refresh the page. This was the purpose: not to refresh the page.
Its not whether you use GET or POST that matters. To avoid refreshing the page you need Ajax, no submits. As was pointer out by u_munder, you are ,missing the logic to fire the Ajax call - ie.button click (not submit)

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.