0

Problem 1: My content overlaps itself twice after I post some data back to the same page using jQuery.ajax(). The reason why I'm posting data back to the same page is because I need to pass my JavaScript values to the PHP side.

Question: How do I edit my code such that there will only be 1 copy of my content, before and after posting of data to the same page?


Problem 2: You may have noticed there is a $("#test").html(data); in my bingo function and a <span id="test"></span> in my body. I can't seem to remove them if not the passing of Javascript values to the PHP side would not work as shown by my print_r().

Question: Is there any way I can remove them but still pass my values from JavaScript to PHP using jQuery.ajax()?


bingo.php

<html>
    <head>
            <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
            <?php
            if (!isset($_POST['varA']) && !isset($_POST['varB']))
            {
            ?>
                <script type="text/javascript">
                $(document).ready(bingo);

                function bingo()
                {
                    jQuery.ajax({
                        type: "POST",
                        data:  {varA: "123", varB: "456"},
                        success: function(data)
                        {
                            alert("POST to self is successful!");
                            $("#test").html(data);
                        }
                    });
                }
                </script>
                <?php
            }
            else
            {
                print_r($_POST['varA']);
                echo " - ";
                print_r($_POST['varB']);
            }
            ?>
    </head>
    <body>
        <input type="text" value="meow"/>
        <span id="test"></span>
    </body>
</html>

2 Answers 2

2

Omg that is so messy! Try the following code anyway:

<?php 
if (isset($_POST['varA']) && isset($_POST['varB'])) {
    print_r($_POST['varA']);
    echo " - ";
    print_r($_POST['varB']);
} else {
?>
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(bingo);

            function bingo()
            {
                jQuery.ajax({
                    type: "POST",
                    data:  {varA: "123", varB: "456", ajax: true},
                    success: function(data)
                    {
                        alert("POST to self is successful!");
                        $("#test").html(data);
                    }
                });
            }
        </script>
    </head>
    <body>
        <input type="text" value="meow"/>
        <span id="test"></span>
    </body>
</html>
<?php
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Oh god, magic! =x By the way, is it all right if I remove that ajax: true?
Yeah sorry, forgot to remove when I later realized that no another parameter is required :)
1

If you wish to keep your ! in your conditions, you can do it the other way round also.

<?php 
if (!isset($_POST['varA']) && !isset($_POST['varB'])) {
?>
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(bingo);

            function bingo()
            {
                jQuery.ajax({
                    type: "POST",
                    data:  {varA: "123", varB: "456"},
                    success: function(data)
                    {
                        alert("POST to self is successful!");
                        $("#test").html(data);
                    }
                });
            }
        </script>
    </head>
    <body>
        <input type="text" value="meow"/>
        <span id="test"></span>
    </body>
</html>
<?php
}
else {
    print_r($_POST['varA']);
    echo " - ";
    print_r($_POST['varB']);
}
?>

1 Comment

It seems I can only select 1 answer to a question, thank you for your help too :)

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.