Click to See Complete Forum and Search --> : noobie, form validation


peach
12-24-2004, 07:08 AM
heres the code:

if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here.
} else {

}
}
?>

How do I tell the script to post the form to dynaform.php .. so that it is emailed. The form action in the is this:
<form method="post" action="<?php echo $PHP_SELF; ?>">

Then the php form validator prevents the form from being submitted and after the if statement you can tell it what to do when the form is validated.

96turnerri
12-24-2004, 07:38 AM
whats wrong with the code you posted?

peach
12-24-2004, 07:42 AM
theres nothing wrong, but after "else" I want to add something:

POST to dynaform.php

Just like you would say action="dynaform.php method="post"
but I dont know php so I don't know how to formulate it.

The snippet of php code says:

if there are no errors in the form, do something with it
And what needs to be done should be specified after the if statement

96turnerri
12-24-2004, 07:50 AM
example of how to post to another page/URL


function post($host, $path, $data) {
$http_response = '';
$content_length = strlen($data);
$fp = fsockopen($host, 80);
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-rlencoded\r\n");
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
while (!feof($fp)) $http_response .= fgets($fp, 28);
fclose($fp);
return $http_response;
}

$postdata = '?foo=bar';
foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val;

$http_response = post('example.org', '/path/to/script.php', $postdata);
?>

peach
12-24-2004, 07:57 AM
I have no clue of what to do with that. Im not really into php. (yet)

i tried to put some things but cant get it to work

?>

and for anyone who cares heres the complete code:

<?php
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
if ($_POST['name']=='')
$arrErrors['name'] = 'Vul uw naam in.';
if ($_POST['achternaam']=='')
$arrErrors['achternaam'] = 'Vul uw achternaam in.';
if ($_POST['email']=='')
$arrErrors['email'] = 'Vul uw email in.';
if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here.
} else { $http_response = 'dynaform.php', $postdata);

}
}
?>

96turnerri
12-24-2004, 08:01 AM
function post($host, $path, $data) {
$http_response = '';
$content_length = strlen($data);
$fp = fsockopen($host, 80);
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-rlencoded\r\n");
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
while (!feof($fp)) $http_response .= fgets($fp, 28);
fclose($fp);
return $http_response;
}

if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here.
} else {
foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val;
$http_response = post('YOURDOMAIN.COM', '/path/to/script.php', $postdata);
}
?>

replacing YOURDOMINA.COM and /path/to/script.php to that of your page dynaform.php

peach
12-24-2004, 08:18 AM
I dont know if you're joking or something. That script you gave me just crashed my browser and computer.
The page gave me an infinite stream of errors.

Or did I implement it wronglt?

<?php

function post($host, $path, $data) {
$http_response = '';
$content_length = strlen($data);
$fp = fsockopen($host, 80);
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-rlencoded\r\n");
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
while (!feof($fp)) $http_response .= fgets($fp, 28);
fclose($fp);
return $http_response;
}


// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
if ($_POST['name']=='')
$arrErrors['name'] = 'Vul uw naam in.';
if ($_POST['achternaam']=='')
$arrErrors['achternaam'] = 'Vul uw achternaam in.';
if ($_POST['geboortedag']=='')
$arrErrors['geboortedag'] = 'Vul uw geboortedag in.';
if ($_POST['geboortemaand']=='')
$arrErrors['geboortemaand'] = 'Vul uw geboortemaand in.';
if ($_POST['geboortejaar']=='')
$arrErrors['geboortejaar'] = 'Vul uw geboortejaar in.';
if ($_POST['adres']=='')
$arrErrors['adres'] = 'Vul uw adres in.';
if ($_POST['postcode']=='')
$arrErrors['postcode'] = 'Vul uw postcode in.';
if ($_POST['woonplaats']=='')
$arrErrors['woonplaats'] = 'Vul uw woonplaats in.';
if ($_POST['telefoonnummer']=='')
$arrErrors['telefoonnummer'] = 'Vul uw telefoonnummer in.';
if ($_POST['email']=='')
$arrErrors['email'] = 'Vul uw email in.';
if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here:

foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val;
$http_response = post('designity.nl', 'dynaform.php', $postdata);
} else {

}
}
?>

96turnerri
12-24-2004, 11:42 AM
hmm what sort erorrs you getting?

change
$http_response = post('designity.nl', 'dynaform.php', $postdata);
to
$http_response = post('designity.nl', '/dynaform.php', $postdata);

peach
12-24-2004, 11:48 AM
like this, and then it continues with the line 41 error

Warning: fsockopen(): unable to connect to designity.nl:80 in /home/designity.nl/www/test5.php on line 34

Warning: fputs(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 35

Warning: fputs(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 36

Warning: fputs(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 37

Warning: fputs(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 38

Warning: fputs(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 39

Warning: fputs(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 40

Warning: feof(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 41

Warning: fgets(): supplied argument is not a valid stream resource in /home/designity.nl/www/test5.php on line 41

96turnerri
12-24-2004, 11:52 AM
hmmm not sure why that, have you tried using www.designity.nl instead of designity.nl in the post function call?

peach
12-24-2004, 11:55 AM
Ill look at it tomoroww.

I dont understand why I would need so mucht code though.
all i want to do is post the info to the file.
if it can be done with a simple action and method attribute, why woul I suddenly need 10 lines of weird code to do it again.

Are you sure that code doesnt do a lot more then just post?

96turnerri
12-24-2004, 12:18 PM
ummmm tomorrows christmas day ;)

peach
12-25-2004, 06:22 AM
Yes, but I have a deadline waiting for me:)

mm still cant get it to work... HELP! :p

drythirst
12-27-2004, 09:32 AM
What is it that you are trying to do? I cannot test the script if it has no variables to process, because the variables are unknown.

peach
12-27-2004, 12:22 PM
the problem is already solved.

I only needed to put in (require once) :)

96turnerri
12-29-2004, 01:13 PM
ummm require once? can i see your complete code, i fail to see how require_once() would fix this problem

peach
12-29-2004, 01:18 PM
voila


<?php
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
if ($_POST['name']=='')
$arrErrors['name'] = 'Vul uw naam in.';
if ($_POST['achternaam']=='')
$arrErrors['achternaam'] = 'Vul uw achternaam in.';
if ($_POST['geboortedag']=='')
$arrErrors['geboortedag'] = 'Vul uw geboortedag in.';
if ($_POST['geboortemaand']=='')
$arrErrors['geboortemaand'] = 'Vul uw geboortemaand in.';
if ($_POST['geboortejaar']=='')
$arrErrors['geboortejaar'] = 'Vul uw geboortejaar in.';
if ($_POST['adres']=='')
$arrErrors['adres'] = 'Vul uw adres in.';
if ($_POST['postcode']=='')
$arrErrors['postcode'] = 'Vul uw postcode in.';
if ($_POST['woonplaats']=='')
$arrErrors['woonplaats'] = 'Vul uw woonplaats in.';
if ($_POST['telefoonnummer']=='')
$arrErrors['telefoonnummer'] = 'Vul uw telefoonnummer in.';
if ($_POST['email']=='')
$arrErrors['email'] = 'Vul uw email in.';
if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here.
require_once ("dynaform.php");
} else {

}
}
?>

then the form:

<form name="form" method="post" action="<?php echo $PHP_SELF; ?>" onSubmit="return checkme();">

<!--
For every form field, we do the following...

Check to see if there's an error message for this form field. If there is,
add the formerror class to the surrounding paragraph block. The formerror
class contains the highlighted box.

Insert the contents of what the user submitted bak into the form field.

Check again to see if this field has an error message. If it does, show
the error icon and the error message next to the field.
-->
<input type='hidden' name='rec_mailto' value=***>
<input type='hidden' name='rec_subject' value='ArtPack bestelformulier'>
<input type='hidden' name='rec_thanks' value='bedankt.htm'>
<INPUT TYPE ="HIDDEN" NAME="OK_PAGE" VALUE="bedankt2.htm">

<div id="myform">
<div id="myform2">
<center><img src="h6.gif" width="196" height="31" border="0" alt="Vul hier onder uw informatie in"></center>
<br />

<div class="row">
<span class="label">
Voornaam:</span>
<span class="formw"><input <?php if (!empty($arrErrors['name'])) echo ' class="formerror"'; ?> id="name" type="text" name="name" size="30" value="<?php echo $_POST['name'] ?>" />
<?php if (!empty($arrErrors['name'])) echo '<br /><span class="errortext">'.$arrErrors['name'].'</span>'; ?>
</span>
</div>

<div class="row">
<span class="label">Tussenvoegsel:</span>
<span class="formw"><input type="text" name="tussenvoegsel" size="30" value="" /></span>
</div>

<div class="row">
<span class="label">Achternaam:</span>
<span class="formw"><input <?php if (!empty($arrErrors['achternaam'])) echo ' class="formerror"'; ?> id="achternaam" type="text" name="achternaam" size="30" value="<?php echo $_POST['achternaam'] ?>" />
<?php if (!empty($arrErrors['achternaam'])) echo '<br /><span class="errortext">'.$arrErrors['achternaam'].'</span>'; ?>
</span>
</div>

<div class="row">
<span class="label">Gebooortedatum:</span>
<span class="formw">
<input <?php if (!empty($arrErrors['geboortedag'])) echo ' class="formerror"'; ?> type="" name="geboortedag" id="geboortedag" maxlength="2" value="<?php echo $_POST['geboortedag'] ?>" size="3">-
<input <?php if (!empty($arrErrors['geboortemaand'])) echo ' class="formerror"'; ?> type="" name="geboortemaand" id="geboortemaand" maxlength="2" value="<?php echo $_POST['geboortemaand'] ?>" size="3">-
<input <?php if (!empty($arrErrors['geboortejaar'])) echo ' class="formerror"'; ?> type="" name="geboortejaar" id="geboortejaar" maxlength="4" value="<?php echo $_POST['geboortejaar'] ?>" size="4">
<?php if (!empty($arrErrors['geboortedag'])) echo '<br /><span class="errortext">'.$arrErrors['geboortedag'].'</span>'; ?>
<?php if (!empty($arrErrors['geboortemaand'])) echo '<br /><span class="errortext">'.$arrErrors['geboortemaand'].'</span>'; ?>
<?php if (!empty($arrErrors['geboortejaar'])) echo '<br /><span class="errortext">'.$arrErrors['geboortejaar'].'</span>'; ?>
</span>
</div>

<div class="row">
<span class="label">Adres:</span>
<span class="formw"><input <?php if (!empty($arrErrors['adres'])) echo ' class="formerror"'; ?> type="text" name="adres" id="adres" size="30" value="<?php echo $_POST['adres'] ?>" />
<?php if (!empty($arrErrors['adres'])) echo '<br /><span class="errortext">'.$arrErrors['adres'].'</span>'; ?>
</span>
</div>

<div class="row">
<span class="label">Postcode:</span>
<span class="formw"><input <?php if (!empty($arrErrors['postcode'])) echo ' class="formerror"'; ?> type="text" name="postcode" id="postcode" size="30" value="<?php echo $_POST['postcode'] ?>" />
<?php if (!empty($arrErrors['postcode'])) echo '<br /><span class="errortext">'.$arrErrors['postcode'].'</span>'; ?>
</span>
</div>

<div class="row">
<span class="label">Woonplaats:</span>
<span class="formw"><input <?php if (!empty($arrErrors['woonplaats'])) echo ' class="formerror"'; ?> type="text" name="woonplaats" id="woonplaats" size="30" value="<?php echo $_POST['woonplaats'] ?>" />
<?php if (!empty($arrErrors['woonplaats'])) echo '<br /><span class="errortext">'.$arrErrors['woonplaats'].'</span>'; ?>
</span>
</div>

<div class="row">
<span class="label">Telefoonnummer:</span>
<span class="formw"><input <?php if (!empty($arrErrors['telefoonnummer'])) echo ' class="formerror"'; ?> id="tel" type="text" name="telefoonnummer" id="telefoonnummer" size="30" value="<?php echo $_POST['telefoonnummer'] ?>" />
<?php if (!empty($arrErrors['telefoonnummer'])) echo '<br /><span class="errortext">'.$arrErrors['telefoonnummer'].'</span>'; ?>
</span>
</div>

<div class="row">
<span class="label">Email:</span>
<span class="formw"><input <?php if (!empty($arrErrors['email'])) echo ' class="formerror"'; ?> type="text" name="email" id="email" size="30" value="<?php echo $_POST['email'] ?>" alt="email|1" />
<?php if (!empty($arrErrors['email'])) echo '<br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?>
</span>
</div>



<div class="row">
<span class="formw"><input type="checkbox" name="algemenevoorwaarden"> Ik accepteer de <A class="dash" HREF="javascript:popUp('algemenev.htm')">Algemene Voorwaarden.</A></span>
</div>
<br /><br /></div><br />
<div id="myform3">



<center>
<img src="h8.gif" width="350" height="47" border="0" alt="Kijk uw informatie nog eens na, en druk op verstuur...">
<br />
<div class="row">
<input type="submit" name="Submit" value="Submit">
</center>
</div>

</div>
</div>
</div>
</Form>

96turnerri
12-29-2004, 03:57 PM
i see ;)