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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

<?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";


?>

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.