Siddarth
Siddarth

Reputation: 21

PHP error with HTML formring

I am learning PHP from a book called PHP and MySQL web development. I am a newbie to PHP , I am pretty well versed with C and HTML, I find syntax of PHP to be pretty same of C.

Look at the code:

<html>
<form action = "processorder.php" method = "post">
<p>tires:<input type=”text” name=”tireqty” size=”3” maxlength=”3” /></p>
<input type=”submit” value=”Submit Order” /></td>
</form>
</html>

This is the HTML code now I will type in the php code and save it as processorder.php

<?php
$tireqty = $_POST[‘tireqty’];
echo "<p>Your order is as follows: </p>";
$tireqty." tires<br />";
?>

After doing this , i go to the html page where I have to enter the value for tires and click submit after doing this it redirects to me mu processorder php page where my raw php code gets displayed and not the proper output which is:

Your order is as follows:
2 tires

But i get a different o/p which is as shown below:

 $tireqty = $_POST[‘tireqty’];
    echo "<p>Your order is as follows: </p>";
    $tireqty." tires<br />";

what is going on here what is wrong??? and i also get an error message is processorder script saying that:

parse error: undefined index 'tireqty' in line something and undefined var tireqty

Hey guys i dont know whats wrong but cant post comments that button is not working or my browser is not enabling me to write comments, I am answering to all the calls here:

Yeah I am using XAMPP what is wrong in that, what should i do?

I copied your codes and yes there are no errors in it but when i go to html page and click submit the browser opens a dialog box saying that "I have chosen to open process.php" and what should it do with it so i select open with gedit because that is the only default option available there, and it redirects to my php script page :(:(:(

WHat is this it is so tough, Is PHP really that difficult or am i working on a poor platform and learning from a bad book?

Is there any good book on php

Upvotes: 2

Views: 851

Answers (4)

user399666
user399666

Reputation: 19889

Always make sure that your variables have been set before you use them:

<?php
    if(isset($_POST['tireqty'])){

        $tireqty = $_POST['tireqty'];
        echo "<p>Your order is as follows: <br />";
        echo $tireqty." tires.</p>";

    }
 ?>

Also, your HTML had a random closing table column tag in it:

<html>
<form action = "processorder.php" method = "post">
<p>tires:<input type="text" name="tireqty" size="3" maxlength="3" /></p>
<input type="submit" value="Submit Order" />
</form>
</html>

Upvotes: 3

Alex
Alex

Reputation: 1304

Always use

if ( isset($_POST['var']) ) { ... }

Same with $_GET, $_SESSION etc. You variable tireqty isn't initialized until form is submitted, that's why throws errors

Upvotes: 0

Dorvalla
Dorvalla

Reputation: 5240

<?php
$tireqty = $_POST[‘tireqty’]; 
echo "<p>Your order is as follows: </p>";
$tireqty." tires<br />";
?>

You closed your echo tags in the first 2nd row already, therefor you need to add another echo tag to the 3rde line.

<?php
$tireqty = $_POST[‘tireqty’]; 
echo "<p>Your order is as follows: </p>";
echo $tireqty." tires<br />";
?>

Upvotes: 5

Rainer.R
Rainer.R

Reputation: 458

Sounds like your PHP environment is not working as it should. Do you have a web server with PHP running or XAMP (http://www.apachefriends.org/en/xampp.html)?

Upvotes: 2

Related Questions