Jump to content

[SOLVED] [php] I can't execute this script, something wrong with variables


ironmaiden666

Recommended Posts

I've got this script:

<html>

<head>

<title>Hi User</title>

</head>

<body>

<h1>Hi User</h1>

<?php

if (empty($userName)){

print <<<HERE

<form>

Please enter your name:

<input type = "text"

name = "userName"><br>

<input type = "submit">

</form>

HERE;

} else {

print "<h3>Hi there, $userName!</h3>";

} //end

?>

</body>

</html>

 

 

I've got apache 2.2.4 and php 5.2.3

I type in my name but nothing happens and my name disappears...

usually I have to write $_POST[something]

For a start you need to have <form action='yourscript.php' method='post'> that deals with recieving the data.  Then you need to assign the data to the $var like $username = $_POST['username']; in your script and make it global so your html can find it by putting global $username at the top of yourscript.php

 

Please go and read w3c schools on html forms and php so you will understand

<html>
<head>
<title>Hi User</title>
</head>
<body>
<h1>Hi User</h1>
<?php
$userName = $_GET['userName'];
if (empty($userName)){
print "contents";
?>

<form>
Please enter your name:
<input type = "text"
name = "userName">

<input type = "submit">
</form>
<?
} else {
print "<h3>Hi there, $userName!</h3>";
} //end
?>
</body>
</html>

I tried these also but they don't work out I tried your tips but in vain...

this one is ok

 

<html>

<head>

<title>Checkbox Demo</title>

</head>

<body>

<h1>Checkbox Demo</h1>

<h3>Demonstrates checkboxes</h3>

<form method ="post"

action ="checkDemo.php">

 

<h3>What would you like with your order?</h3>

<ul>

<li><input type ="checkbox"

name ="chkFries"

value ="1.00">Fries

</li>

<li><input type ="checkbox"

name ="chkSoda"

value =".85">Soda

</li>

<li><input type ="checkbox"

name ="chkShake"

value ="1.30">Shake

</li>

<li><input type ="checkbox"

name ="chkKetchup"

value =".05">Ketchup

</li>

</ul>

<input type ="submit">

</form>

</body>

</html>

 

but this one not...

 

<html>

<head>

<title>Checkbox Demo</title>

</head>

<body>

<h3>Demonstrates reading checkboxes</h3>

<?php

print <<<HERE

chkFries: $chkFries <br>

chkSoda: $chkSoda <br>

chkShake: $chkShake <br>

chkKetchup: $chkKetchup <br>

<hr>

HERE;

$total = 0;

if (!empty($chkFries)){

print ("You chose Fries <br> \n");

$total = $total + $chkFries;

} // end if

if (!empty($chkSoda)){

print ('You chose Soda <br> \n");

$total = $total + $chkSoda;

} // end if

if (!empty($chkShake)){

print ('You chose Shake <br> \n');

$total = $total + $chkShake;

} // end if

if (!empty($chkKetchup)){

print ("You chose Ketchup <br> \n');

$total = $total + $chkKetchup;

} // end if

print "The total cost is \$$total \n";

?>

</body>

</html>

 

 

<?php
$total = 0;
if (isset($_POST['chkFries'])){
print ("You chose Fries \n");
$total = $total + $_POST['chkFries'];
} // end if
if (isset($_POST['chkSoda'])){
print ("You chose Soda 
\n");
$total = $total + $_POST['chkSoda'];
} // end if
if (isset($_POST['chkShake'])){
print ("You chose Shake 
\n");
$total = $total + $_POST['chkShake'];
} // end if
if (isset($_POST['chkKetchup'])){
print ("You chose Ketchup 
\n");
$total = $total + $_POST['chkKetchup'];
} // end if
print "The total cost is \$ ".$total ." \n";


?>

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.