Page 1 of 1

Submit form - display error message on the same page

Posted: Thu May 20, 2010 3:52 pm
by Jony
When I want to submit a form, the data from the form is submited in a script.php script on a different page, but i want the error message(s) resulted from that script.php file to be displayed on the current page. I cannot figure this out, i tryed using header("Location: url"); in the script.php file but it seems that this works only if the script is not returning anything. I also tryed using

Code: Select all

<?php include_once 'script.php';
echo $msg; ?>
where $msg is the error msg from the script.php but this actually redirects me to the blank page with the name of the script.php.

I can work this out if i put the script in the same page and call action="page.php" but i would like to keep the scripts separate from HTML. If there is a way for doing this, please tell me or show me how to do it.

Regards.

Re: Submit form - display error message on the same page

Posted: Thu May 20, 2010 4:07 pm
by hallsofvallhalla
i am not understanding exactly what you are trying to do here. So message is being defined in script.php? How is the data being submitted? In script.php? if it is a get or post method then where is it being sent?

I am little confused. Maybe showing us some of your script.php will help.

Re: Submit form - display error message on the same page

Posted: Thu May 20, 2010 4:24 pm
by Jony
- Yes, the error message is defined in script.php
- The data is submitted through a form on page.php like this <form method="post" action="script.php">
- The method used $_POST like this: $data=$_POST['input']; and it is located in script.php
- In the script.php the $data is then "cleaned" and then encoded and inserted into database or if there is something wrong, there is assigned an $errMsg="ERROR!" or something like that.

I want that error message to be displayed on page.php without leaving page.php after the submit button is clicked. I can achieve this, if I write the script on the page.php, but not if i write it separately in script.php. I don't know how to do this if the script is written separately.

Re: Submit form - display error message on the same page

Posted: Thu May 20, 2010 5:08 pm
by Torniquet
why would you want to keep the pages seperate if you want the errors to be placed on the page your submitting from...


keep it simple...

at the top of your page.

Code: Select all

$errorMessage = "";

if(isset($_POST['submitbuttonname'])){

//do your registration script in here

if(isset($_POST['username'])){
///lalalala
} else {
$errorMessage = "error";
}
}
then lay your form out liek so.

Code: Select all

<form action="" method="post">
<?php echo $errorMessage; ?><br />
username:<input type="text" name="username" /><br />
password:<input type="password" name="password" /><br />
<input type="submit" name="submit" value="register" />
</form>

Re: Submit form - display error message on the same page

Posted: Thu May 20, 2010 5:20 pm
by hallsofvallhalla
ah I see, yes there is no reason to separate the pages that I see, the PHP cannot be read by view source.

Also you are running script.php, so the code that has include will not run. It never runs the actual echo $msg, because you are calling script.php instead.

Re: Submit form - display error message on the same page

Posted: Thu May 20, 2010 9:00 pm
by Jony
hallsofvallhalla wrote:ah I see, yes there is no reason to separate the pages that I see, the PHP cannot be read by view source.

Also you are running script.php, so the code that has include will not run. It never runs the actual echo $msg, because you are calling script.php instead.

I got used with c++ object oriented programming, defining functions in a different file and using them later by including the .h, etc
I have no problem in writing the php code on the same file where i build the html pages, but I was just curious if in this case what i wanted could be done.


@Torniquet... Thanks for the advice: i'm going to keep the things simple. :)

Re: Submit form - display error message on the same page

Posted: Fri May 21, 2010 12:00 am
by hallsofvallhalla
you can still separate functions, but it doesn't seem like you are doing that here, you can keep it separated as long as you change your form to go to the original script with the include script.php instead of trying to go script.php directly. Define the get and post in the original file.