1

I have a form which contains one text field. Now, I'm trying to echo out the contents of that input after the form was submitted. Here's my code for the same:

<script>    
    function postResultss()
    {
        document.write("<?php echo ($_POST['tweet1']); ?>");
    }
</script>

<form method = "POST">
    <input type = "text" name = "tweet1">
    <br>
    <input type = "submit" onclick = "postResultss()" />
</form>

The above code is inside a PHP file. However, nothing gets echoed out on submitting the form. The function does get called as expected, because I have tried echoing custom messages while debugging. However, nothing gets echoed out when I try to echo the value of $_POST['tweet1'], where tweet1 is the name of the input text field whose contents I'm trying to display.

What seems to be wrong here?

7
  • PHP inside JS. mmmmm, no. Commented Aug 22, 2015 at 18:16
  • @HawasKaPujaari And why not? Commented Aug 22, 2015 at 18:16
  • 2
    Firstly, because the button is a submit button the attached javascript function postResultss() is unlikely to get called - unless it has event.preventDefault() in the js function. Secondly, the POSTed var will NOT be available to the js function at the point of clicking the button - only AFTER the form has been submitted and event then this is the wrong approach to show theposted values. Commented Aug 22, 2015 at 18:18
  • @RamRaider Well, when I try to echo something like echo ("felicity"); inside my JS function, the message does get displayed. Which means that the JS function is indeed being called on submitting the form. Commented Aug 22, 2015 at 18:20
  • OK, so the js function gets called but the POSTed variables will not be available at the time of clicking the button Commented Aug 22, 2015 at 18:23

3 Answers 3

2

You do a submit and onclick. That goes wrong. Further, don't do a document.write!

Do this as better alternative (no php in js):

<?php
   if ($_SERVER['REQUEST_METHOD'] == 'POST') // check if post
      echo htmlentities($_POST['tweet1']);
?>

<form method="post">
    <input type="text" name="tweet1">
    <br>
    <input type="submit" value="Tweet!">
</form>
Sign up to request clarification or add additional context in comments.

Comments

1

Rather than use javascript to write the content which, in your example wouldn't work, use php to generate the response for the user to see

<form method = "POST">
    <input type = "text" name = "tweet1">
    <br>
    <input type = "submit" value='Submit' />
    <div id='msgs'>
    <?php
        if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['tweet1'] ) ){
           echo $_POST['tweet1'];
        }
    ?>
    </div>
</form>

Comments

0

The problem is that the javascript function is called before the form submits (so before php can print out the echo), and since the page updates the document.write is being overwritten from the new request.

Why dont you try something like this?

<form method = "POST">
    <?php echo ($_POST['tweet1']); ?>
    <input type = "text" name = "tweet1">
    <br>
    <input type = "submit"/>
</form>

or:

<script>
var text = "<?php echo ($_POST['tweet1']); ?>";
if(text != ""){
   alert(text);
}
</script>
<form method = "POST">

    <input type = "text" name = "tweet1">
    <br>
    <input type = "submit"/>
</form>

4 Comments

or a document write instead of the alert
This would work, but I need to call out a function on submitting the form, so this solution may not be helpful in my case.
wait so do you want the text to be display after or before submitting the form?
After submitting the form. That's why I was trying to trigger the onclick event in my submit field.

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.