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
https://forums.phpfreaks.com/topic/32773-code-error-for-learning-sake/
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
[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]
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*.
[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]
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.