?
Solved

php help

Posted on 2003-03-12
10
Medium Priority
?
247 Views
Last Modified: 2008-02-26
Hi,
   I am new to php and am having some problem with it. I am trying to make an address book sort of a thing. There is hyperlink in my page...the link is to the page itself and an id gets transferred through the link. I obtain the id by using
$id=$_GET['id'];

This works but not when the page is opened for the first time(i.e when the id has empty value). I get this on my page
Notice: Undefined index: id in C:\Webdev\AddressBook\firstpage.php on line 18

However,when the id is passed the notice doesnt come. Do you guys have any idea on this one. It would be of great help if so. So then till next time take care and happy coding.
Sid :)

   
               
0
Comment
Question by:Sid123
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:PCBackup
ID: 8125224
In PHP, any undefined variables cannot be referenced in any statement, unless you precede them with an "@" character, the "at" sign, as in

  print @$id;

This allows the script to gracefully attempt the reference without generating the error.  You showed that you get the error on line #18, which is probably later in the script than the line you provided...

  $id=$_GET('id');

In your script, this line probably returns a null result and does not set the $id variable.  Then when you try to reference it, it generates the error.

You can either test for this case and set it to some value that can be tested or use the @ prefix to prevent the error.  You can perform a test for a good value with something like this...

  if (@$id <> "") {
     ...a functional program block...
    }

That will prevent any further errors from not having a value for $id.

Gene
0
 
LVL 6

Accepted Solution

by:
carchitect earned 80 total points
ID: 8125809
hi use this code

if(isset($_GET['id']))
$id=$_GET['id'];
else
$id=""; //whatever you want
0
 
LVL 6

Expert Comment

by:carchitect
ID: 8125810
use this code on top of the page
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:jfisbein
ID: 8127317
You can change you php.ini settings to not show NOTICE errors.

Change your "error reporting" conf to:
error_reporting = E_ALL & ~E_NOTICE

Or you can put an "@" before call to an statement that can cause an error.
ex:

@$id=$_GET['id'];
0
 
LVL 15

Expert Comment

by:VGR
ID: 8131973
follow carchitect's suggestion
0
 
LVL 1

Expert Comment

by:PCBackup
ID: 8133780
We have not heard back from Sid123 yet, so let's just wait and see what he (?) ends up deciding.

I personally don't like setting variables that I don't need, so I prefer to use the @ method to just prevent errors during execution.  Forcing the variable to be set to something just uses up extra server memory, and in a large scale web server, that's going to be important when you have many visitors all accessing pages at the same time.

Ultimately, the PHP programmer must decide what's most important to them.

Gene
0
 

Author Comment

by:Sid123
ID: 8133994
Hi all,
   Thank you very much for the help. I have tried out isset and it's working all right.However, I have come up with a new problem using session variables. I have two pages firstpage.php and firstpage1.php.The codes are as following-

--firstpage1.php--
 <? session_start();
  session_register("strError");
  $strError="1";

   if($name=="")
   { $strError ="Please enter the name!";
     $redirect = "firstpage.php";
     return header("location:$redirect");
     }
      ?>
--firstpage.php--
 <? session_start();
  session_register("strError");?>

<font color=#FF0000 size="4">&nbsp;<? if ($strError != "1")
                    echo $strError;     ?></font>

however I get this
Notice: Undefined variable: strError in C:\Webdev\AddressBook\firstpage.php on line 41
Notice: Undefined variable: strError in C:\Webdev\AddressBook\firstpage.php on line 42

From what I came to understand this is some sort of a bug...something to deal with the versions but I am not very sure about it. What do you guys say...how should I go about getting my codes right? So till next time take care and happy coding.

Sid:)

0
 
LVL 6

Expert Comment

by:carchitect
ID: 8134071
file1.php

<?
session_start();
str="";
session_register('str');
$_SESSION['str']="anything";
header("Location: file2.php");
?>


file2.php
<?
session_start();
echo $_SESSION['str'];
?>

and whenever use session variable use $_SESSION['variablename'];
0
 

Author Comment

by:Sid123
ID: 8145819
Hi,
  I tried to use the mail function.The thing is I am pretty vague about this.Can I use it to send email using the local host or is it possible only when the file is uploaded. I read somewhere that you can send email using local host too...but you need an SMTP server...and I have downloaded it(freeware). I tried to mail to "someone@yahoo.com" but and getting this-

Warning: mail() [function.mail]: SMTP server response: 550 User not local. We don't relay in C:\Webdev\AddressBook\email1.php on line 10

Can anyone give me any idea on this one? So then till next time take care and happy coding.
Sid:)
0
 
LVL 15

Expert Comment

by:VGR
ID: 8145896
yes, that's 100% normal. The "From:" address you specified has to be verified and validated by your SMTP server (the one you talk to with mail(), specified in the php.ini [mail] section, at least on Windows, and in server's configuration on *nix)

This address HAS TO BE VALID, ie to belong to your domain.

Otherwise it would be the wide-open door to would-be-spammers everywhere in the world, sending email as from "nobody-dont-even-search@nonexistentdomain.com"

understood ? :D
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question