I have the following form:
HTML
<form id="myForm" action="" method="post">
<div id="edit-box">
<INPUT type="submit" name="save-text" id="save-text" value="Save">
<textarea id="editor" name="editor">
<?php echo $content; ?>
</textarea>
</div>
</form>
PHP
if ($_POST["save-text"]){
// get form data and do stuff here
}
How can I make sure that the PHP script is run only when the submit button is clicked and no other time?
The problem is that
if($_POST["save-text"])returns false and so your code isn’t being executed. You will need to wrap the$_POST["save-text"]with theisset()function. isset() can be used to check the existence of a certain variable and returnsTRUEif a variable exists and has a value other thanNULLorFALSE.The correct code would look like this:
You should also make sure your submit buttons have a ‘value’ attribute. If they don’t, there wont be a value in the $_POST array and so isset($_POST[“submit”]) would return false.
Example: