开发者

PHP Posting Form back in header

开发者 https://www.devze.com 2023-03-28 16:58 出处:网络
In my web site, I\'m doing form checks on server side. When an unexpected input comes from the user i want to post the form back where it comes from. I\'ve found some user defined classes and function

In my web site, I'm doing form checks on server side. When an unexpected input comes from the user i want to post the form back where it comes from. I've found some user defined classes and function on php.net but they didn't work. Can anyone help me about this?

Assume i have a addCompany.php page and it has a form. And I have a second 开发者_运维问答page action.php. The form in addCompany.php is sent to action.php page. I check the form for validation I see an error aand i want to post the form back to the addCompany.php. Normally I can send it by querystring however it is not safe because of the character limit. Therefore, i want to sent the form back while i'm redirecting like this header('Location:addCompany.php')


The usual procedure is to process the form on the same page and if the validation is ok you redirect to the next stage, otherwise continue back to the form.

Here's a pseudocodish example:

<?php
// form.php

if( isset( $_POST[ 'submit' ] ) && form_validates( $_POST ) ) {
    // do what you want with the data
    // redirect to the next stage
    header( 'Location: next.php' );
    die();
}

// didn't validate or no submit yet, show the form again

if( isset( $_POST[ 'email' ] ) ) {
    $email = $_POST[ 'email' ];
}
else {
    $email = '';
}
?>
<form action="form.php"> <!-- submit to same page -->
<input type="text" name="email" value="<?php echo $email; ?>" />
<?php if( /* email field didn't validate */ ) {
    echo 'Please enter a valid email address';
} ?>
<input type="submit" name="submit" />
</form>
0

精彩评论

暂无评论...
验证码 换一张
取 消