Jump to content

Code error - for learning sake


freakykiwi

Recommended Posts

[code]<html>
<head>
<title> Name test </title>
</head>

<body>
Hi <?php echo htmlspecialchars ($_POST['name']) ;?>
<p> you are <?php echo (int) $_POST['age'] ;?> years old!</p>
<p><?php if (strpos($_POST['name'], 'Harley') !==FLASE)
{echo 'Hey champ! Welcome';}
else {
echo 'hi Stranger/Other, how are you';
}?></p>
</body>
</html>
[/code]

I want it to say 'Hey champ! Welcome' when Harley was inputted, but 'Hi Stranger/Other' when another name was put it. with this code 'Hi champ, Welcome' comes up for everybody

~Freakykiwi
Link to comment
Share on other sites

I wasn't thinking and I'd written the post in textedit copied/pasted :S

My question was, is that I want it to say 'Hey champ! Welcome' when Harley was inputted, but 'Hi Stranger/Other' when another name was put it. with this code 'Hi champ, Welcome' comes up for everybody
Link to comment
Share on other sites

[code]<html>
<head>
<title> Name test </title>
</head>

<body>
Hi <?php echo htmlspecialchars ($_POST['name']) ;?>
<p> you are <?php echo (int) $_POST['age'] ;?> years old!</p>
<p><?php 
if ($_POST['name'] == 'Harley'){
    echo 'Hey champ! Welcome';
}else{
   echo 'hi Stranger/Other, how are you';
}
?></p>
</body>
</html>
[/code]
Link to comment
Share on other sites

Try this:
[code]
<html>
<head>
<title> Name test </title>
</head>

<body>
Hi <?=htmlspecialchars($_POST['name'])?>
<p> you are <?=intval($_POST['age'])?> years old!</p>
<p><?php
if($_POST['name'] == 'Harley'){
    echo 'Hey champ! Welcome';
}else{
  echo 'hi Stranger/Other, how are you';
}
?>
</p>
</body>
</html>
[/code]

It works for me. What you had already also works on my server. *shrug*.
Link to comment
Share on other sites

[code]
<?php
$name = $_POST['name'];
$age = $_POST['age'];

if(!is_numeric($age)){
die("The age is not numeric");
}

if(!ctype_alnum($name)){
die("Your name contained illegal characters");
}

echo "$name
<p> you are $age years old!<br></p>
<p>";
if($name == 'Harley'){
    echo 'Hey champ! Welcome</p>';
}else{
  echo 'hi Stranger/Other, how are you</p>';
}
?>
[/code]
Link to comment
Share on other sites

And you've saved that file as action.php?

I've had problems in the past with error reporting and using die.
Try this and see if anything shows up.

Otherwise, comment everything out and add lines back in until it stops working. That will show you the problematic line.
[code]
if(!is_numeric($age)){
print "The age is not numeric";
die();
}

if(!ctype_alnum($name)){
print "Your name contained illegal characters";
die();
}[/code]
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.