Jump to content

Can't Override the Default Value of the Textbox

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
makamo66

makamo66

    Newbie

  • Members
  • PipPip
  • 18 posts
I have an html textbox that was written like this:

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:

$_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.

#2
Drew

Drew

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Makamo66

What you have done is perfectly correct, there is no issue with it. You have the "value" set to "1", so it will give you "1". If you don't set the "value", then it will give you nothing back.

Try this.

if !(isset($_POST['qtyBox'])
{
$qtyBox = 1;
}
else
{
$qtyBox = $_POST['qtyBox'];
}
echo "<form action='".$SERVER['PHP_SELF']."' method='POST'>";
echo "<b>Quantity:</b> <input type='text' name='qtyBox' value='$qtyBox' size='1' />";
echo "<input type='submit' value='Send Qty' />";
echo "</form>";



(The above code does work. I use it all the time.)

Edited by Drew, 19 May 2010 - 03:14 PM.


#3
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
can you post the whole form code? And are you sure that the form is submitted via post?

#4
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
The code posted by AndyW should work.

#5
Drew

Drew

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
If you don't want POST, then the forms information will appear in the location bar upon posting. This can allow eople to do whatever they want to a form at times. So use GET with care.

if !(isset($_GET['qtyBox'])
{
$qtyBox = 1;
}
else
{
$qtyBox = $_GET['qtyBox'];
}
echo "<form action='".$SERVER['PHP_SELF']."' method='GET'>"; //Set to GET so can see what is actually being passed.
echo "<b>Quantity:</b> <input type='text' name='qtyBox' value='$qtyBox' size='1' />";
echo "<input type='submit' value='Send Qty' />";
echo "</form>";