Can't Override the Default Value of the Textbox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Can't Override the Default Value of the Textbox

Post by makamo66 »

I have an html textbox that was written like this:

Code: Select all

echo "<b>Quantity:</b> <input type=\"text\" name=\"qtyBox\" value=\"1\" size=\"1\" />";
When the form is submitted I collect the value of the textbox like this:

Code: Select all

	$_SESSION['qtyBox'] = $_POST['qtyBox'];
	$qtyBox = $_SESSION['qtyBox'];
	echo $qtyBox;
The problem is that no matter what I type into the text box, the value is echoed out as 1. If I leave the value blank and type a number into the box, I get a blank too.
Last edited by Benjamin on Tue May 18, 2010 4:38 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Can't Override the Default Value of the Textbox

Post by JakeJ »

When your echo is surrounded by double quotes, replace all of the double quotes except the first and the last with single quotes as follows.

Code: Select all

echo "<b>Quantity:</b> <input type='text' name='qtyBox' value='1' size='1' />";
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Can't Override the Default Value of the Textbox

Post by flying_circus »

JakeJ wrote:When your echo is surrounded by double quotes, replace all of the double quotes except the first and the last with single quotes as follows.

Code: Select all

echo "<b>Quantity:</b> <input type='text' name='qtyBox' value='1' size='1' />";
Why? Escaping the double quotes as he is doing will work fine and is acceptable when encapsulated within double quotes. I believe the HTML spec says to encapsulate element attributes in double quotes as oposed to single quotes.

makamo66:
mocking up your code, the following will put out whatever I have typed in the input box.

Code: Select all

echo $_POST['qtyBox']; 
Are you sure your form is set to method post?
Are you starting your session at the top of the script?
Are you only executing this block of code when the request method is a post?

What happens if you just echo out the post value, as I've done above?
Post Reply