Reputation: 1
I am trying to figure out the problem in this code but no luck. When I click on the form submission it doesn't generate any alert even its not echo anything. Please have a look on the code and let me know if there is anything I am missing`
<form action="" method="post">
<textarea rows="6" cols="70" name="notes" placeholder="Enter Notes"></textarea><br>
<input type="submit" name="submit1" id="submit1" class="upload-button" style="margin-left:40%" value="Post" />
</form>
<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}`
?>
Upvotes: 0
Views: 2745
Reputation: 5670
You have a backtick at the end of your php. Just remove it. The code works as expected once the backtick is removed.
<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}` <=== right HERE
?>
Upvotes: 1
Reputation: 7617
Something like this should very well work (which is not so far from what you did).
<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}
?>
<section>
<form action="" method="post">
<textarea rows="6" cols="70" name="notes"
placeholder="Enter Notes"></textarea><br>
<input type="submit" name="submit1"
id="submit1" class="upload-button"
style="margin-left:40%" value="Post" />
</form>
</section>
Side-Note: If your intent is not to submit the Form but to see an
Alert
Message once the button is clicked, you may do something like this:
<section>
<form action="" method="post">
<textarea rows="6" cols="70" name="notes"
placeholder="Enter Notes"></textarea><br>
<input type="submit" name="submit1"
id="submit1" class="upload-button"
style="margin-left:40%" value="Post" />
</form>
</section>
<?php
// ECHO OUT RAW JAVASCRIPT
// TO PREVENT DEFAULT SUBMIT-BUTTON BEHAVIOUR WHEN CLICKED.
echo '<script type="text/javascript">';
echo "function stopBehaviour(evt) {";
echo "evt.preventDefault();";
echo 'alert("This is alert");';
echo "}";
echo "document.getElementById('submit1').addEventListener( 'click', stopBehaviour, false );";
echo '</script>';
?>
Notice the Placement of the PHP Code in both instances... especially the latter... However, be clear that your Form will never get submitted as you are preventing the Button's Default behaviour... and there would also be no need to check if the form was submitted or not (using PHP)... The earlier Approach above would submit the Form but once the Form gets Submitted, PHP automatically adds a Javascript code which will stall the entire Form-Submission Trip till the Alert
message gets run and your ewuifbhfiueruigriug
gets written to the Stream...
Upvotes: 0
Reputation: 11
<?php
if(isset($_POST['submit1'])) {
echo '<script type="text/javascript">';
echo 'alert("This is alert");';
echo '</script>';
echo 'ewuifbhfiueruigriug';
}
echo '<form action="" method="post">';
echo '<textarea rows="6" cols="70" name="notes" placeholder="Enter Notes"></textarea><br>';
echo '<input type="submit" name="submit1" id="submit1" class="upload-button" style="margin-left:40%" value="Post" />';
echo '</form>';
?>
If you look at the end of the code for the php submit, there's a ' that shouldn't be there in the OP.
Upvotes: 0