Jump to content

Two problems...


jamesjmann

Recommended Posts

First

Since I first started learning PHP, I've been writing code for forms like this:

 

$name = $_POST["name"];

 

if ($name == "") {

echo "You didn't type anything";

}

 

I've written scripts like this and tested them on a GOdaddy server, and they always worked fine.

 

Now, after testing scripts like that on my own server (WAMP2 [php5, Mysql, Apache]), I get an error saying "Unidentified index: name", and so I have to nest that if statement AS WELL AS the variable assignment inside another if statement like this:

 

if (isset ($_POST["name"])) {

$name = $_POST["name"];

if ($name == "") {

echo "you didn't type anything.

}

}

 

The error doesn't actually stop the script from running, but a medium sized box appears in the browser with the error message contained that's after the element in the document of which is getting an invalid error.

 

Which way is truly the correct way? Because now that I have to use the second way in order for my scripts to run correctly on my own server, I have to write more code and it all gets disorganized and more complicated to read.

 

So...is this a configuration thing that I can tweak in WAMP2 to stop messages like that from popping up? Or is the second way the way you're supposed to do it?

 

Also, I was wondering why ereg got deprecated and what the replacement of it is in PHP5? I've asked before and even googled it but I simply get taken to a "Pearl" function on PHP.net. I'm not sure what it is, or how to use it, and haven't got any hints from either PHP.net or anywhere else.

 

Any suggestions?

Link to comment
Share on other sites

The error has always been there, you just didn't have error reporting set to a high enough level when developing, or you didn't have display_errors On. You should check that a variable is defined before attempting to use its value.

 

Thanks so much! That's what I figured, but I wasn't sure, and of course I had to be sure, so I could determine whether or not I should contrive alternative ways of organizing my code.

Link to comment
Share on other sites

if (isset($_POST["name"]) && $_POST["name"] != "") {
//Do whatever
} else {
echo "Error!";
}

 

Well, the thing is I have multiple error checks for each field as well as unique messages for each error and for each field.

 

I like doing it that way, because I feel like I have more control, but the whole isset thing really complicates things, and as a result, my script is like greek. It's been this way ever since I tried implementing javascript to work with the very same form, so a first would be to contrive a possible way to organize the code so as to increase readability and most of all, organizationalibility (yes, I realize I just made up a word lol).

 

It's really tricky, because there are several ways of which I could go about this...

 

I could nest all field checks within the isset check, or I could write an isset check for each field check, which would make my script longer than it should have to be. But even the isset kinda confuses me, so BLAH

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.