Jump to content

[SOLVED] Where's my error?


Scooby08

Recommended Posts

I have a form that I'm trying to validate.. I have 3 files involved: index.php; process.php; functions.php.. When submitted it runs through process.php.. In that file I call on a function from the functions.php file to check for errors, but my problem is that the function is not actually getting called.. For some reason it is overlooked and I can't figure out why.  Here is the function code..

 

<?php
function check_for_errors() {  
if ($_REQUEST['firstname'] == '') {
	$message  = "Please enter firstname!" . "<br />";
}
if ($_REQUEST['lastname'] == '') {
	$message .= "Please enter lastname!" . "<br />";
}
return $message;
header("Location: index.php?message=$message");
exit();
}
?>

 

What am I overlooking here? Or maybe I'm just not supposed to call on validate form function like that?? Im not sure??

 

 

Link to comment
Share on other sites

be sure that ur process.php file has on top this code

<?php
     include("functions.php");
?>

 

Also make sure ur index file has

<?php
     include("process.php");
?>

 

Next make sure that inside index.php ur form header is right.

Something like this

<form name="form1" method="post" action="" onsubmit="return check_for_errors()"

Link to comment
Share on other sites

be sure that ur process.php file has on top this code

<?php
     include("functions.php");
?>

 

Also make sure ur index file has

<?php
     include("process.php");
?>

 

Next make sure that inside index.php ur form header is right.

Something like this

<form name="form1" method="post" action="" onsubmit="return check_for_errors()"

 

Including process.php in index.php is not necessary. You just keep process.php as your form action.

Link to comment
Share on other sites

Here is the form action line

 

<form action="process.php?action=add" method="post">

 

Here is process.php

 

<?php
// Connect to database
include ('config.inc.php');
include ('database.inc.php');
include ('functions.inc.php');

// Actions
function main() {
if ($_REQUEST['action'] == 'add'){
	add();
}
elseif ($_REQUEST['action'] == 'update'){
	update();
}
elseif ($_REQUEST['action'] == 'delete'){
	delete();
}
}

// Call actions function
main();

function add() {
check_for_errors();
}
?>

Link to comment
Share on other sites

:) .. Ok, Now what you should do is,

 

1) First print a test line before you call the function add() just to check whether it enter inside the if condition.

2) If yes, then make a same check before you call function check_for_errors().

3) If that test also passed, then remove header inside your function and print $message.

 

I think you will be able to troubleshoot the problem in this manner.

 

By the way, what is the exact error it gives?

Link to comment
Share on other sites

It passes both those tests and gets inside the add() function.. It's just not recognizing the check_for_errors() function.. I have info below that from the form that is inserted into the database, so I know it's getting there, but that dang function is hiding for some reason.. If I take the code out of the function it works fine.. I might just end up doing it that way..

Link to comment
Share on other sites

I guess I just have one more question then... I am passing the error messages through the url.. Is this the way to do this?? Seems like that string could get really long being that I have a message for each field that errored.

 

Also, when I get the string form the url and display it on the page it is displayed as so:

 

Please enter the Bride\\\'s name!

 

Then I added this:

 

$message = stripslashes(mysql_escape_string($_REQUEST['message']));

 

Which still left one slash like so:

 

Please enter the Bride\'s name!

 

So I added another stripslashes around the other stripslashes:

 

$message = stripslashes(stripslashes(mysql_escape_string($_REQUEST['message'])));

 

That got rid of all slashes, but that seems like stripslash overkill.. Is this the correct way to do that as well??

 

Link to comment
Share on other sites

It passes both those tests and gets inside the add() function.. It's just not recognizing the check_for_errors() function.. I have info below that from the form that is inserted into the database, so I know it's getting there, but that dang function is hiding for some reason.. If I take the code out of the function it works fine.. I might just end up doing it that way..

 

Buddy, Your code works very fine on my PC!! I don't know what the problems there!!! And again I am saying, your header portion inside the function wont work, you have to take that in ur mind.

Link to comment
Share on other sites

if you are wanting to redirect when an errors occurs then your best off having all your logic in a seperate script.

 

in the processing scipt, you no output. you store the errors in an array then when you wan to output the errors you store the array in a session then redirect to the appropriate page and use a loop to display each of the errors

 

processing script:

<?php

//start session
session_start();

//array & variable declaration
$errorarray = array();	//used to store error messages

//validate your input 
if (1 == 1) {
$_SESSION['errorstate'] = 1;
$errorarray[] = "1 ='s 1";
}

if (1 != 1) {
$_SESSION['errorstate'] = 1;
$errorarray[] = "1 does not equal 1";
}

//check for errors
if ($_SESSION['errorstate'] == 1) {
$_SESSION['errormessage'] = $errorarray; //store the errorarray in a session
header('location: page.php'); //redirect accordingly
}

?>

 

main page:

<?php

if((isset($_SESSION['errorstate'])) && ($_SESSION['errorstate'] == 1)) { //check for the sessions and if there is an error

foreach($_SESSION['errormessage'] as $key => $value){ //print the errors
	echo $value.'<br />';
}
//clear the errors
$_SESSION['errorstate'] = "";
$_SESSION['errormessage'] = "";

}

?>

Link to comment
Share on other sites

Hey thanks a bunch for that bit of code!! Now I've never used a session before.. The code didn't seem to output anything from copy paste.. I had to add the session_start() to the main page as well, and before the html, and it seemed to work. The question I have is this.. I take it the session cannot start in the process.php and carry over to the main page.. I must start a session on the main page before the process for it to be correct then? And do sessions need to be specified on all pages? And do they need to be specified in the function itself, or can it just be at the top of that process page as well?

Link to comment
Share on other sites

Now I'm trying to add an if success message..  Here's what I have, but I'm not getting it..

 

<?php
//check for errors
if ($_SESSION['errorstate'] == 1) { // if error
    $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session
    //header('location: page.php'); //redirect accordingly
    header("Location: ".SCRIPT_ADD."");
} elseif ($_SESSION['errorstate'] == 0) { // if no error
    $errorarray[] = "You have successfully added a new customer!";
    $_SESSION['errormessage'] = $errorarray; //store the errorarray in a session
    //header('location: page.php'); //redirect accordingly
    header("Location: ".SCRIPT_ADD."");
}	
?>

Link to comment
Share on other sites

well, the thing is I've never been a great fan of using SESSIONS here and there. For your case, I prefer the following method

 

mypage.php

$errcode=0;
if(error type 1 occurs){
           $errcode=1;
}elseif(error type 2 occurs){
           $errcode=2;
}elseif(error type 3 occurs){
           $errcode=3;
}

if($errcode>0){
      header('Location: index.php?errcode='.$errcode)
}

 

errcodelist.php

$errcodearr[]=""; // as we have no error for error code zero
$errcodearr[]="1st error message";
$errcodearr[]="2nd error message";
$errcodearr[]="3rd error message";

 

index.php

include ("errcodelist.php");

if(isset($_GET['errcode'])){
     $errcode=$_GET['errcode'];
     echo $errcodearr[$errcode];
     exit();
}

 

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.