/    Sign up×
Community /Pin to ProfileBookmark

noobie, form validation

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.

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@96turnerriDec 24.2004 — whats wrong with the code you posted?
Copy linkTweet thisAlerts:
@peachauthorDec 24.2004 — 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
Copy linkTweet thisAlerts:
@96turnerriDec 24.2004 — example of how to post to another page/URL

[code=php]
function post($host, $path, $data) {
$http_response = '';
$content_length = strlen($data);
$fp = fsockopen($host, 80);
fputs($fp, "POST $path HTTP/1.1rn");
fputs($fp, "Host: $hostrn");
fputs($fp, "Content-Type: application/x-www-form-rlencodedrn");
fputs($fp, "Content-Length: $content_lengthrn");
fputs($fp, "Connection: closernrn");
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);
?>
[/code]
Copy linkTweet thisAlerts:
@peachauthorDec 24.2004 — 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);

}
}

?>
Copy linkTweet thisAlerts:
@96turnerriDec 24.2004 — [code=php]
function post($host, $path, $data) {
$http_response = '';
$content_length = strlen($data);
$fp = fsockopen($host, 80);
fputs($fp, "POST $path HTTP/1.1rn");
fputs($fp, "Host: $hostrn");
fputs($fp, "Content-Type: application/x-www-form-rlencodedrn");
fputs($fp, "Content-Length: $content_lengthrn");
fputs($fp, "Connection: closernrn");
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);
}
?>[/code]


replacing YOURDOMINA.COM and /path/to/script.php to that of your page dynaform.php
Copy linkTweet thisAlerts:
@peachauthorDec 24.2004 — 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.1rn");

fputs($fp, "Host: $hostrn");

fputs($fp, "Content-Type: application/x-www-form-rlencodedrn");

fputs($fp, "Content-Length: $content_lengthrn");

fputs($fp, "Connection: closernrn");

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 {

}

}

?>
Copy linkTweet thisAlerts:
@96turnerriDec 24.2004 — hmm what sort erorrs you getting?

change

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

to

$http_response = post('designity.nl', '/dynaform.php', $postdata);
Copy linkTweet thisAlerts:
@peachauthorDec 24.2004 — 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
Copy linkTweet thisAlerts:
@96turnerriDec 24.2004 — hmmm not sure why that, have you tried using www.designity.nl instead of designity.nl in the post function call?
Copy linkTweet thisAlerts:
@peachauthorDec 24.2004 — 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?
Copy linkTweet thisAlerts:
@96turnerriDec 24.2004 — ummmm tomorrows christmas day ?
Copy linkTweet thisAlerts:
@peachauthorDec 25.2004 — Yes, but I have a deadline waiting for me?

mm still cant get it to work... HELP! :p
Copy linkTweet thisAlerts:
@drythirstDec 27.2004 — 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.
Copy linkTweet thisAlerts:
@peachauthorDec 27.2004 — the problem is already solved.

I only needed to put in (require once) ?
Copy linkTweet thisAlerts:
@96turnerriDec 29.2004 — ummm require once? can i see your complete code, i fail to see how require_once() would fix this problem
Copy linkTweet thisAlerts:
@peachauthorDec 29.2004 — 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>
Copy linkTweet thisAlerts:
@96turnerriDec 29.2004 — i see ?
×

Success!

Help @peach spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...