Jump to content

[SOLVED] after submitting a form half the page doesnt load


rickphp

Recommended Posts

Hi,

 

When the form has passed all validation I am using the die function to display a page saying the form has been sent etc. However only half the page loads, it seems that the footer file is not loaded although the header file is loaded. If i paste the contents of the footer file within the die command the page appears as normal. This is a temp fix though as this causes a problem because when I update my footer file I would need to keep manually updating my contact forms footer data.

 

Is there a solution to this? Can i somehow include the footer file as part of the die command or something like this?

 

Hope some one can help

 

Thanks

Link to comment
Share on other sites

no. that's just the most common (and wrong) use for it.  die (and exit) just tell php to stop the script.  You can optionally have it do something on exit by putting it as an argument.  The idea is that if something goes wrong, stop everything and display an error.  The reason why that's the wrong thing to do, is that simply stopping your script will more than likely give your user virtual blue balls.  In other words, that is not a way for your system to gracefully handle errors. 

 

For instance, let's say you have an online store.  It's a multi-step process.  halfway through the process, some error happens.  So instead of your script saying "oh crap, something happened, let's save what the user has done so far, redirect to something else, try something else, notify someone, etc..." it just dies. User is like "wtf..." and leaves.  You just lost a sale and even worse, you have no idea why.

Link to comment
Share on other sites

I f*cking love the die() function. I use it all the time - generally to display errors to the user if they try and access something they're not supposed to.

 

rather than doing this:

 

if($user == 'allowed')
{
//do whatever
}
else
{
echo 'denied';
}

 

You can do it with much less, neater looking code like this:

 

if($user != 'allowed')
{
die('denied');
}
//rest of script

Link to comment
Share on other sites

You would abstract the error handling/logging into functions or a class or something and just invoke it. With your code, you have no idea what caused the error.  Was it something with the script? Did the user cause it? On accident or purpose? 

 

It could be a legitimate bug in your system that caused your script to do that.  User complains to you and you have nothing to look at, so you're stuck trying to reproduce the error with no info except for what the user happens to remember/tell you. Which, if you are still relying on just dumping a die statement to the screen like that, no offense, but you must not have much experience trying to get info out of users..

 

Same thing if user did it on purpose.  Let's say he's trying to crack your system.  You have no idea what exactly he's trying and where, because you are not creating any kind of paper trail with that.  That paper trail can mean the difference between you finding a hole before he does and plugging it, and picking up the pieces from whatever havoc he caused, because he had all the time in the world to poke at your stuff and you were blissfully unaware.

 

Link to comment
Share on other sites

That was just an example, what I use is a function that I call whenever I need to display an error, which in turn dies, displaying the appropriate error message.

 

I don't really see how you can display a more specific error message than one that's written specifically for that error.

Link to comment
Share on other sites

The point is not to just display an error message.  The point is to log everything you can about what happened and where, and instead of just displaying an error message and halting the script completely, you do something else like redirect the user somewhere else, have your script try to complete some action a different way, etc... something other than just saying "you had an error" and then quitting. 

 

Going back to the online store: The goal is to get the user to buy something, right?  Well if in the middle of shopping some error occurs, and all you have is a die('there was an error') ...what do you think the chances of that visit turning into a successful purchase is?  I personally would immediately go somewhere else.  How can I trust that your script won't somehow mess up my personal info, especially my cc info, if it's just dying on some random other error? 

 

And even if I somehow wasn't worried about that.  What am I supposed to do next? Am I supposed to start over? What happened to the items in my shopping cart? Are they still there?  Do I tell someone about it?  Did my order go through or do I need to resend it? I don't want to get charged twice.  There are a million wtf happens next questions, depending on what happened where, that simply dumping a "there was an error" and then quitting the script, just isn't good enough. 

 

Websites aren't built for the sake of being built.  Not if you want to make money off it, anyways.  So you can't just say oh well, something happened, display a message, the end. 

Link to comment
Share on other sites

Yeah, but the only place I actually have errors is when the user is attempting something they shouldn't. My scripts work fine, I've never had an unintended error. And for all E_USER_ERRORs I have a custom error handler which logs the error and prompts the user to continue. I'm unsure as to what else I can do to improve error handling...

Link to comment
Share on other sites

Yeah, but the only place I actually have errors is when the user is attempting something they shouldn't. My scripts work fine, I've never had an unintended error. And for all E_USER_ERRORs I have a custom error handler which logs the error and prompts the user to continue. I'm unsure as to what else I can do to improve error handling...

 

Okay well that's a step in the right direction.  As far as whether you should use die for when users are attempting something they shouldn't...well it really depends on the site, how much money is being invested in it/flowing through it, as to how much effort you want to put into it.  For instance, with a site that collects user's personal info, especially financial info like cc numbers etc.., I would never let the user know that they are trying to do something they shouldn't do.  All you are doing is giving them a clue of where your script's boundaries are. 

Link to comment
Share on other sites

Well, I'm 16 and I made my own forum/profile/music/blog site; I'm not really doing it for the money, and I don't collect anyone's bank account numbers :P

 

But yeah, I see your point.

 

Yeah, back on topic though, I think the best thing you could do is make a custom error function, with some of the elements that Crayon Violent suggested, such as a redirect or something. Or maybe say something like "There was a problem, can you please fill in these details again".

Link to comment
Share on other sites

  die(include('footer.php'));

 

Works a treat, but I also need to include some text.

 

I.e. Form subimitted bla bla bla. and then have the footer file included.

 

Having two die functions doesnt work, is there another way of achieving this?

 

 
die ("<br /><b>Thank you $name1.<br/ ><br />Your message has been received and will be answered shortly.</b><br /></div></div>");

die(include('footer.php'));

 

Thanks

 

 

Link to comment
Share on other sites

Thats worked perfectly! Job done.

 

Now I've created myself another problem. Ideally if the user doesnt pass the form validation, i.e. they miss a required field I want it to display an error on a page without the form showing (like the thank you msg), I've done this, but when there is an error the page shows fine but when you try to go back it doesnt let me go back.

 

I am using a php session to remember the contents of the entered fields, so I assume this is causing the issue.

 

I have also noticed that when the form is sent, if you leave the page and go back to the page it shows the thank you page (until the session expires anyway). Is there a way to overcome this?

 

Im using the following to retain the data:

 

<?php
session_start();
?>

  if ($submit) {

  foreach($_POST as $key=>$value){
    $_SESSION[$key]=$value;
  }

<input class="form" name="name1" type="text" size="26" maxlength="20" value="<? print("$_SESSION[name1]");?>" />

 

Any further help would be appreciated.

Link to comment
Share on other sites

I've decided having the error echoed is actually better for me. As for the thank you message showing up after the form was submitted when leaving the page and going back it was because i forgot to close the session, doh! Closing the session after succesful completion of the form has resolved the problem.

 

Thank you for all your help!

Link to comment
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.