PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kydude1957
Forum Commoner
Posts: 31 Joined: Sun Sep 14, 2014 1:37 am
Post
by kydude1957 » Tue Sep 23, 2014 8:48 pm
I was running this code in WAMP and I got undefined index errors on all the if get page lines so then I added the isset to each line and the errors went away. Now the menu which works perfectly on my web hosting site has stopped working. On page load the home page displays fine, then if you click resume it loads the resume page but the portfolio and contact pages wont load when you click the corresponding links on the menu.
Code: Select all
<?php
include("includes/header.html");
include("includes/navbar.html");
if(isset($_GET['page']) == "resume"){
include("includes/resume.php");
}else if(isset($_GET['page']) == "portfolio"){
include("includes/portfolio.html");
}else if(isset($_GET['page']) == "contact"){
include("includes/contact.php");
}else
include("includes/home.php");
include("includes/footer.html");
?>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Sep 23, 2014 9:22 pm
isset() only tells you if the thing exists. True/false. You still have to do the regular == check. As in
Code: Select all
if(isset($_GET['page']) && $_GET['page'] == "resume"){
If it is set then it'll do the condition and if it isn't set then it won't do the condition (and thus not trigger the warnings you were dealing with before).
kydude1957
Forum Commoner
Posts: 31 Joined: Sun Sep 14, 2014 1:37 am
Post
by kydude1957 » Tue Sep 23, 2014 9:36 pm
thanks. that fixed the resume and portfolio page issue but the contact page still has undefined index. Current code:
Code: Select all
<?php
include("includes/header.html");
include("includes/navbar.html");
if(isset($_GET['page']) && $_GET['page'] == "resume"){
include("includes/resume.php");
}else if(isset($_GET['page']) && $_GET['page'] == "portfolio"){
include("includes/portfolio.html");
}else if(isset($_GET['page']) && $_GET['page'] == "contact"){
include("includes/contact.php");
}else
include("includes/home.php");
include("includes/footer.html");
Error:
Notice: Undefined index: action in C:\wamp\www\MySite\includes\contact.php on line 4
Call Stack
# Time Memory Function Location
1 0.0000 135536 {main}( ) ..\index.php:0
2 0.0010 148120 include( 'C:\wamp\www\MySite\includes\contact.php' ) ..\index.php:14
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Sep 23, 2014 11:13 pm
So... how about posting the code? Could isset() be useful there?
kydude1957
Forum Commoner
Posts: 31 Joined: Sun Sep 14, 2014 1:37 am
Post
by kydude1957 » Tue Sep 23, 2014 11:44 pm
Code: Select all
<?php
$action=$_REQUEST['action'];
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'austen@austenrobinson.net'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP Contact Form with JQuery Validation</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<script src="http://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script>
<script src="jquery.validate.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#contactform").validate();
});
</script>
<style type="text/css">
body {
font-family:Arial, Tahoma, sans-serif;
}
#contact-wrapper {
width:430px;
border:1px solid #e2e2e2;
background:#f1f1f1;
padding:20px;
}
#contact-wrapper div {
clear:both;
margin:1em 0;
}
#contact-wrapper label {
display:block;
float:none;
font-size:16px;
width:auto;
}
form#contactform input {
border-color:#B7B7B7 #E8E8E8 #E8E8E8 #B7B7B7;
border-style:solid;
border-width:1px;
padding:5px;
font-size:16px;
color:#333;
}
form#contactform textarea {
font-family:Arial, Tahoma, Helvetica, sans-serif;
font-size:100%;
padding:0.6em 0.5em 0.7em;
border-color:#B7B7B7 #E8E8E8 #E8E8E8 #B7B7B7;
border-style:solid;
border-width:1px;
}
</style>
</head>
<body>
<div id="contact-wrapper">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<label for="name"><strong>Name:</strong></label>
<input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
</div>
<div>
<label for="email"><strong>Email:</strong></label>
<input type="text" size="50" name="email" id="email" value="" class="required email" />
</div>
<div>
<label for="subject"><strong>Subject:</strong></label>
<input type="text" size="50" name="subject" id="subject" value="" class="required" />
</div>
<div>
<label for="message"><strong>Message:</strong></label>
<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
</div>
<input type="submit" value="Send Message" name="submit" />
</form>
</div>
</body>
</html>
kydude1957
Forum Commoner
Posts: 31 Joined: Sun Sep 14, 2014 1:37 am
Post
by kydude1957 » Tue Sep 23, 2014 11:48 pm
line 4 in the code above error message:
( ! ) Notice: Undefined index: action in C:\wamp\www\MySite\includes\contact.php on line 4
Call Stack
# Time Memory Function Location
1 0.0000 135536 {main}( ) ..\index.php:0
2 0.0010 148120 include( 'C:\wamp\www\MySite\includes\contact.php' ) ..\index.php:14
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Sep 24, 2014 1:43 am
And line 4 has you trying to access something in $_REQUEST that does not exist. Just like how earlier you were trying to access something in $_POST that did not exist.
The answer is also to use isset().
kydude1957
Forum Commoner
Posts: 31 Joined: Sun Sep 14, 2014 1:37 am
Post
by kydude1957 » Wed Sep 24, 2014 6:23 am
Tried this but it isnt correct.
Code: Select all
$action=if(isset($_REQUEST['action']));
kydude1957
Forum Commoner
Posts: 31 Joined: Sun Sep 14, 2014 1:37 am
Post
by kydude1957 » Wed Sep 24, 2014 11:22 am
Also tried this:
Code: Select all
if(isset($_REQUEST)){$action=$_REQUEST['action'];}
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Wed Sep 24, 2014 11:55 am
Code: Select all
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Sep 24, 2014 12:22 pm
Code: Select all
if(isset($_REQUEST)){$action=$_REQUEST['action'];}
That will avoid the warning, yes, but in doing so it only defines $action sometimes. You need $action defined always. What Celauran posted will get you a value every time: either the value in $_REQUEST if it exists, or null if it doesn't.