0

On form submit, I want to give the user a message.

Originally, I was doing

if(isset($_POST['submit'])) {
echo "submitted"; 
}

But this would appear randomly at the top of the page.

I want control where the message is output, so I wanted to append the message to a DOM element... to do that, I thought I could use JavaScript as so:

if(isset($_POST['submit'])) {
    echo "<script type=\"text/javascript\">
            document.getElementById(\"submitmsg\").innerHTML = \"submitted\";
          </script>";
}

The HTML shows that the PHP seems to output the JS correctly, but submitmsg is empty.

enter image description here

Any thoughts?


HTML form: calls itself so it can run the PHP code at the top of the page:

<form role="form" action="" method='post' accept-charset='UTF-8'>
    <div class="row">
        <div class="form-group col-xs-12 floating-label-form-group">
            <input class="form-control" type="text" name="name" placeholder="Name">
        </div>
    </div>
    <div class="row">
        <div class="form-group col-xs-12">
            <button type="submit" name="submit" class="btn btn-lg btn-success">Send</button>
        </div>
    </div>
</form>
5
  • Are you echoing it before or after the main html? Commented Jun 13, 2014 at 0:51
  • 4
    When your browser encounters a script tag, it immediately executes it. At that time, the browser has not parsed your <p> element yet and thus an error will be thrown (since your javascript is fetching an element that doesn't exist yet). Commented Jun 13, 2014 at 0:51
  • @Bart how do I ensure that the <p> element is loaded before appending the text? Commented Jun 13, 2014 at 0:53
  • bart ment you have to output your p tag first before outputting your js Commented Jun 13, 2014 at 0:54
  • @Growler put the echo statement after the <p> element or use the window.onload event. Commented Jun 13, 2014 at 0:54

3 Answers 3

3

Not sure what you really do since you didn't post all the code, but I see 2 options.

First : On submit, you use an ajax query and don't refresh the page. If that's the case you should use the oncomplete() callback to do the javascript stuff (php should not return javascript code).

Second : The page is reloaded, then you should use PHP to directly echo html wherever you want in your code :

<nav ... >...</nav>
<p id='submitted><?= isset($_POST['submit']) ? "Submitted" : "" ?></p>

Note that you can put php tags where you want really :

<html>
     <head>
           <title><?php echo "Today is " . date('Y-m-d');?></title>
     </head>
     <body>
           <?= "My Cool Body" ?>
     </body>
 <html>
Sign up to request clarification or add additional context in comments.

5 Comments

I think you mean window.onload for native javascript
I like the second option. However, the message is output even after I refresh the page. If I refresh, shouldn't the isset be reset to false?
@Ohgodwhy I was mentionning XMLHttpRequest callbacks
@Growler it depends. Often your web browser will ask you if you want to re-post the request, if you do, then it's a new submit.
@Growler which you can prevent from occuring if you use header('Location: path/to/my/file.php'); and use $_SESSION variables to check the isset.
1

I want control where the message is output

Why don't you store the submit status in a PHP variable and output it where required:

<?php
$is_submitted = isset($_POST['submit']);
?>

<html>
<body>
<!-- your page -->
<nav></nav>

<?php if($is_submitted) : ?>
    <p id="submitmsg">Submitted</p>
<? endif; ?>

Or even just put your isset() check inline further down your page.

But this would appear randomly at the top of the page.

There is no point in echoing out a Javascript call to display this message, you can put PHP where ever you want to in a document.

2 Comments

I'm confused. If PHP is server side and therefore loaded before client-side HTML, wouldn't the PHP be run before the HTML loads anyway? Therefore, how can you "place" PHP further down in the document and have it execute in that order?
PHP is executed server side, and in your case you're generating HTML with it. Once PHP has executed, you have your HTML document which is sent to the client. You can control where you put your PHP generated content by putting it anywhere in your HTML page. The only way this wouldn't work is if you're generating a large amount of your page structure with Javascript (which is unusual).
0

the other answers are pretty much the right way.. if you wanna stick with the way you're doing things now, just switch your php output to:

window.onLoad = document.getElementById("submitmsg").innerHTML = "submitted";

if the Page is reloaded anyway you can echo submitted where you need it.

Comments

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.