Jump to content

Recommended Posts

I have a html form A and a php file  B.

The php file B calls the html form A that allows the user to enter data.

The data entered in the form A is sent to file B via POST action.

The file B needs to check whether some fields are empty?

If B finds that some fields were empty, then it should again reload the html form A with the right entries retained in the form and (wrong entries being cleared).

In this case how to pass the information to the form A.

It is to be noted that B calls A and A passes data to  B via POST as far as possible.

 

 

All kinds of help are appreciated!

Link to comment
https://forums.phpfreaks.com/topic/54073-passing-parameter/
Share on other sites

On file B:

 

<?php

if (!empty($_POST['var1'])){
   $_SESSION['var1'] = $_POST['var1'];
}

?>

 

A.html file:

 

<form action="b.php" method="POST">
   <input type="text" name="var1" value="<?php echo $_SESSION['var1']; ?>">
</form>

 

So basically use sessions. First check if any of the fields are empty, and if there is an empty field register all the non-empty fields as sessions. Then when they go back to the form it will echo the non-empty fields  and leave the empty ones blank.

Link to comment
https://forums.phpfreaks.com/topic/54073-passing-parameter/#findComment-267312
Share on other sites

On file B:

 

<?php

if (!empty($_POST['var1'])){
   $_SESSION['var1'] = $_POST['var1'];
}

?>

 

A.html file:

 

<form action="b.php" method="POST">
   <input type="text" name="var1" value="<?php echo $_SESSION['var1']; ?>">
</form>

 

So basically use sessions. First check if any of the fields are empty, and if there is an empty field register all the non-empty fields as sessions. Then when they go back to the form it will echo the non-empty fields  and leave the empty ones blank.

 

 

A.html will most likely have to be renamed to a.php

 

Unless your server is specifically set up for it, .html files cannot be parsed as php files.

 

 

Also, this will make that variable be displayed every time the page is loaded because it is a session.(after the session is set of course)

 

I do this kind of thing by checking the fields in php once the form is submitted. If it finds an error, it sets an $error[] array with a message and loops through the error messages and displays them, then in the form itself you would do basically what pocobueno said.

 

<?php

if (!empty($_POST['var1'])){
   $error[] = 'This field is is not valid';
}

?>
<?php if(isset($error)){foreach($error as $error){echo $error.'<br>';}} ?>
<form action="b.php" method="POST">
   <input type="text" name="var1" value="<?php if(isset($error)){echo $var1;} ?>">
<input type="text" name="var2" value="<?php if(isset($error)){echo $var2;} ?>">
<input type="text" name="var3" value="<?php if(isset($error)){echo $var3;} ?>">
</form>

 

This will pre-fill in the fields that had data so the user does not have to fill out the form all over again. I sincerely suggest making this conditional upon an $error variable because if the form submits to itself, on a successful entry it will fill in the form again with the data that was just sent.... I have made that mistake a time or two.

 

 

Hope this is clear enough for ya.

 

Nate

 

Link to comment
https://forums.phpfreaks.com/topic/54073-passing-parameter/#findComment-267341
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.