First and foremost, add the following to a separate PHP file:
Language: php (GeSHi-highlighted)
<?php
if (isset($_POST['myinput'])) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="myinput" value="test">
<input type="text" name="myinput2" value="test2">
<input type="submit" name="Submit" value="Submit">
</form>
This will give you an idea of how the $_POST superglobal array, is populated by the HTTP Post data. $_POST is an associative array, with the name of the form elements providing the string keys, and the element values, providing the corresponding array values.
Rather then using multiple submit buttons, I recommend you use a checkbox. It's a much cleaner approach, and makes life much easier server side. Let's look at another example:
Language: php (GeSHi-highlighted)
<?php
if (!empty($_POST)) {
if (isset($_POST['mycheck'])) {
echo "Checkbox is checked";
//do this
}
else {
echo "Checkbox is NOT checked";
//do this instead
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="myinput" value="test">
<input type="checkbox" name="mycheck" value="1">
<input type="submit" name="Submit" value="Submit">
</form>
If the checkbox is checked, it's 'key'=>'value' data will be present in the $_POST array, thus (isset($_POST['mycheck'])) will evaluate as true. If it's not checked, it's data will not be present (or 'set'), and the if construct will evaluate as false.
You may wish for the checkbox to be checked by default, so for example, the script will continue to allow the user to enter more data, until they uncheck it.
<input type="checkbox" name="mycheck" value="1" checked>
Alternatively, you may wish to leave it unchecked by default, with the addition of a "predictive" piece of code. For example, if the user checks the box to add more data, there is a good chance that they will continue to add more data after that.
<input type="checkbox" name="mycheck" value="1" <?php if (isset($_POST['mycheck'])) echo 'checked'; ?>>
Once they have finished adding data, they will then be required to uncheck the box, to finalize their work.
NOTE: if you are working with a valid XHTML document, the correct checkbox syntax is:
<input type="checkbox" name="mycheck" value="1" checked="checked" />
-----
As for your second question, I have had an article in the works for the past few weeks, which would have been ideal for answering this one. Unfortunately, I have not had the time to finish it.
It's important to remember that JavaScript executes in the users browser, it is client side validation, in an environment that you have no control over. As such, you cannot enforce the validation, not without intentionally killing your page if the user has JavaScript disabled.
To cut to the chase, server side validation is ESSENTIAL, where as anything client side, is an optional addition to your validation. You should ideally use PHP to check that every expected key is present, and each keys corresponding value, is of the expected type.
As you are working with databases, the importance is increased ten fold, you do not want to open your script up to an SQL injection attack.
http://us3.php.net/manual/en/function.mysql-real-escape-string.phpBefore the days of AJAX, the only established way for client side to communicate with server side, is via the HTTP Post (ie. a form), or the URL query string. As your data is already in a form, your JavaScript validation should either prevent the submission of the form, if the data is invalid, or let it continue as normal. A
very basic example:
Language: javascript (GeSHi-highlighted)
<script language="JavaScript">
function validateForm() {
if (document.getElementById('myinput').value != 'test') {
return false;
}
return true;
}
</script>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" onSubmit="return validateForm()">
<input type="text" id="myinput" name="myinput" value="test">
<input type="submit" name="Submit" value="Submit">
</form>
There are others who will be able to better advise you in the field of JavaScript, so I am not going to go into much more detail then that. If you find yourself in a position where you want to pursue the use of AJAX, I can help you with the server side part of the process. It's not particularly different from the normal processing of form data, you validate the POST/GET data, run your database query, output the corresponding updated value.
If you have any questions regarding the PHP related details, please feel free to ask.