Jump to content

Recommended Posts

Hi,

I have a complex issue and I have no idea how I can resolve it. Here goes. I have a form with inputs. I validate the inputs. If they are ok, I redirect to success page. All is fine. If it's not successful however (since it's a self calling form), I get back to the page and there I want to output an error message right?

 

Well that's exactly where my problem is. Since the page has called itself back, the HTML is already in place. How am i supposed to get my error msg on the page since the page is already built? In my PHP all I do is create a text field, calling a css class for positioning but obviously since I am not withing the html tag, the positionning is all screwed up. What does this mean? Does this mean that upon validation I have to generate the whole HTML output with my error message in the misdt of it? Or is there a better way? If so can you tell me how?

 

Thanks for the help,

JR

Link to comment
https://forums.phpfreaks.com/topic/152579-self-calling-form-issue-careful-complex/
Share on other sites

The logic flow of your script should be as follows.....

 

* User accesses php script

* Script determines that no data has been submitted via the form yet and displays the form

* User sees blank HTML form in browser, fills it out, and submits it

* User accesses php script again

* Script determines that data has been submitted this time, and processes it accordingly.  If successful, do something with the data and then redirect the user to success page.  If failed, build array of errors to display and display the form again (without redirecting the user)

* User sees form again, but this time it has error messages

All you have to do is change action="THIS_SCRIPT" to the URL for the script you put this code in:

$errors = array();
$processed = array();

// form was submitted
if (isset($_POST['name']))
{
if (empty($_POST['name']))
	$errors['name'] = 'Name cannot be blank.';

if (empty($_POST['msg']))
	$errors['msg'] = 'Message cannot be blank.';

if (empty($errors))
{
	// no errors occurred... do stuff you'd do upon success of form submission
}
}

echo '
<form action="THIS_SCRIPT" method="post">
<table width="100%">';

// there were errors...
if (!empty($errors))
echo '
	<tr>
		<td align="center">The following error(s) occurred:<br />', implode('<br />', $errors), '</td>
	</tr>';

echo '
	<tr>
		<td', isset($errors['name']) ? ' style="color:red;"' : '', '>Name</td>
		<td><input type="text" name="name" value="', isset($_POST['name']) ? $_POST['name'] : '', '"', isset($errors['name']) ? ' style="border:1px solid red;"' : '', ' /></td>
	</tr>
	<tr>
		<td', isset($errors['msg']) ? ' style="color:red;"' : '', '>Message</td>
		<td><input type="text" name="msg" value="', isset($_POST['msg']) ? $_POST['msg'] : '', '"', isset($errors['msg']) ? ' style="border:1px solid red;"' : '', ' /></td>
	</tr>
</table>
</form>';

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.