Forum Moderators: coopster
//stick in form header
onsubmit="return verify(this);"//the check function
function verify(form)
{
if (form.form_username.value=="")
{
alert( "You forgot to complete your USERNAME" );
return false;
}
else
{
return true;
}
}
but in PHP form, so the function cannot be seen.
I'm trying to make a form that checks every field before submitting to the DB. If any field is incorrect the form will notify the user and give them another chance to submit the form, retaining the values entered so far. I haven't found a script for any form that does all these things; if anyone knows of one please sticky me. I've been trying to write one on my own but I was hung up on my question above.
<?php
$csv = "whatever.csv";
if(!$fp = fopen($csv, "a")) die ("Cannot open data file");array_shift($_REQUEST);
$size = sizeof($_REQUEST);
for($i=0; $i<$size; $i++) {
$str = $str . array_shift($_REQUEST);
if(!($i==($size-1))) {
$str = $str . ", ";
}
}
fwrite($fp, $str . "\n\r");
echo "<p>Thank You</p><p>The data was successfully saved.</p>";
?>
as you can see, I'm submitting in to a CSV file. hopefully this isn't a problem. :)
Here's a simple script with a form, check it out to get an idea on how things work.
<form method="post" action="<?php echo $PHP_SELF?>">
<table width="122" border="1" cellspacing="2" cellpadding="0">
<tr>
<td><b>Data:</b></td>
<td><input name="data" size="100" maxlength="255"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Input Data"></td>
</tr>
</table>
</form><?php
if($submit)
{//begin of if($submit).
// Set global variables to easier names
$title = $_POST['data'];
//Check if the "title" field is empty then print error message if necessary.
if(!$title){ //If its really empty.
echo "<br>Data field must not be empty. <br><br><b>- The Management.</b>";
exit();
}// End of if
// If all the required fields are filled, print success message.
echo "<br>It's all good. <br><br><b>- The Management.</b>";
}// End of if $submit
?>
Also, I just checked your script and what it did, instead of writing what was written in the textfield, in wrote the value of the button. :D
if(isset($_POST) && is_array($_POST)) {$errors = array(); // Initialize error array.
// Check for the data file.
if (!$fp = fopen($data["whatever.csv", "a")) { // 0 should be 'n'
$errors[] = 'Cannot open data file.';
}// Check for a name.
if (empty($_POST['name'])) {
$errors[] = 'You forgot to enter your name.';
}if (empty($errors)) { // If everything's okay, submit it!
array_shift($_REQUEST);
$size = sizeof($_REQUEST);for($i=0; $i<$size; $i++) {
$str = $str . array_shift($_REQUEST);
if(!($i==($size-1))) {
$str = $str . ", ";
}
}
fwrite($fp, $str . "\n\r");
echo "<p>Thank You</p><p>The data was successfully saved.</p>";
}} else { // Report the errors.
echo '<strong>Error</strong>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please click the back and try again.</p><p><br /></p>';}
Every time I load the page, even if I haven't submitted the form yet, the error message comes up, but it doesn't actually list any errors. This means there is a problem with the IF statement (if(isset($_POST) && is_array($_POST))); the form thinks I have submitted something when I haven't. Also, even when I have submitted something, the same blank error message comes up. What's wrong here?
PS: Thanks for your help so far. This is exactly the script I want if I can get it to work out.
$errors = array(); // Initialize error array.// Check for the data file.
if (!$fp = fopen($data["whatever.csv", "a")) { // 0 should be 'n'
$errors[] = 'Cannot open data file.';
}
Your script writes the whatever.csv, so there's no reason to check for it, and theres absolutely no way not to be able to open it.
When it comes to the 'name' field being empty, use this, and if you actually need that file open error than just do this, no point in doing an array:
// Check for the data file.
if (!$fp = fopen($data["whatever.csv", "a")) {
echo "Can not open the Data file, please ensure it exists and is Writeable.";
exit();
}
if (empty($_POST['name'])) {
echo "You forgot to enter your name, please go back and try again";
exit();// Stops the script from doing anything else, like writing (to) the CSV.
}
I believe that should work, and get rid of this line;
if (empty($errors)) { // If everything's okay, submit it!
i would opt for the best of both worlds and check the form with javascript first and then with php.
nearly all surfers have javascript turned on - so by checking with javascript first you don't have to force them to load another page to correct their mistakes. there will be a few who have js turned off - you can catch these with the help of Shot above.
my 2c
There is a serious problem with the script; it doesn't retain the values the user enters in the form. So if there's a problem with one of the fields, all the data is erased when the form is refreshed, forcing them to enter the whole thing again. I could remedy this using cookies, but is there an easier way?
Jamie, I am reluctant to use Javascript because then my form's parameters would be visible, and I'd like to keep them private. Also, I plan to give this form a built-in ban function, that is, if it is blatantly obvious the submitter is trying to hack or spam the site, they will be automatically banned by IP. This would best be done in PHP, not JS.
PS: If the submission goes through, how do I redirect the user to the index? I'm guessing I'll have to use headers, but I'm not sure how to have the headers trigger only if the form has been submitted.
This is how you redirect your users to index:
echo "<p>Thank You</p><p>The data was successfully saved.</p>";
echo "<meta http-equiv=Refresh content=3;url=index.php>";
//Refresh content=3 where the number is the amount of seconds.
// Also include a link to go to the index in case the person has META Refresh disabled in the browser.
Hope you get the idea, also, when it comes to the Error displaying, try this:
if($submit)
{// Begin of if.
// Set variables to keep things nice and neat.
$name = $_POST['name'];
// Check for the data file.
if (!$fp = fopen($data["whatever.csv", "a")) {
echo "Can not open the Data file, please ensure it exists and is Writeable.";
exit();
}
if(!$name){
echo "You forgot to enter your name, please go back and try again";
exit();// Stops the script from doing anything else, like writing (to) the CSV.
}
array_shift($_REQUEST);
$size = sizeof($_REQUEST);for($i=0; $i<$size; $i++) {
$str = $str . array_shift($_REQUEST);
if(!($i==($size-1))) {
$str = $str . ", ";
}
}
fwrite($fp, $str . "\n\r");
echo "<p>Thank You</p><p>The data was successfully saved.</p>";
echo "<meta http-equiv=Refresh content=3;url=index.php>";
}
<input type="submit" value="Submit" style="button: #FFFFFF; font: Arial; font-size: 16; color: #000000;">
You'll notice the button is not named. I have chosen not to name it because then the value submit=Submit is injecting into my query string, and I don't want the value "Submit" in my DB.
Also, even if this did work, I'm not sure it would solve the problem I mentioned earlier about the user-entered values. If the user filled out all of the form except the name, after trying to submit the page all the other data would be erased. How do I fix this?
but thanks for the help with the meta refresh; I have learned something new today :)
Try adding name="Submit
I have chosen not to name it because then the value submit=Submit is injecting into my query string, and I don't want the value "Submit" in my DB.
Although MySQL is great, I don't see the need for it in every situation. And I like being able to download easily editable CSV files right to my desktop.
I am having a bit of a problem with something, maybe you can help me out
[webmasterworld.com...]