/    Sign up×
Community /Pin to ProfileBookmark

using form in a table

Hi,

I have a table that is very similar to a shopping cart table. I want to show the quantity in a small box for user to adjust the quantity. However the more I learn anout forms, the less I’m convinced I’m on the right track to use form for this operation. The requirement of the Method and Action parameters suggests that forms are mainly for e-mails. I’ve spent 2-3 days and haven’t found out how to write into a form text box. Any suggestions? Any good references or sample codes? Thanks in advance.

to post a comment
HTML

8 Comments(s)

Copy linkTweet thisAlerts:
@GarySMay 18.2006 — Forms are for capturing user input. Method says how the data will be sent, Action tells the system where to go next. Neither implies email: what happens next is up to you: store in a database, choose the next page to display.... or send an email.

So... what you describe sounds like a perfect application for a form (can't think of any other way to do it!)

Here's the simplest form (in a page called "basket.htm") that will do what you describe:

[code=html]<form action="basket.htm" method="post">
<input name="quantity" value="0" />
<input value="Change quantity" type="submit" />
</form>[/code]


But the value is hard wired... which is not what you want. So let's add a dynamic element:

[code=html]<form action="basket.htm" method="post">
<input name="quantity" value="<?php echo $quantity ?>" />
<input value="Change quantity" type="submit" />
</form>[/code]


To make it work, you need to test for POSTed values [ isset($_POST) ] and update the value of $quantity accordingly.

Hope that's enough to get you going - post back if you need more code.
Copy linkTweet thisAlerts:
@wmwauthorMay 18.2006 — I put together a small routine as in the end to check out what I learned about forms.

I can't tell if the code is even partially working. The 2 echo statements fail. Here is how I hope it will work: This piece of code displays the data and wait for a user to enter a value and press the Submit button. Since the form action is name of the current file, it'll get the data setting, display and wait etc.. Any comments? Thanks,



code:

<?php
$quantity = 0;
if (isset($_POST))
$quantity = $_POST['quantity'];
else
$quantity = 1;
echo "$quantity<br><br>";
?>
<html><body><form action="basket.php" method="get">
<input type = "TEXT" input name="quantity" value="<?php echo $quantity ?>">
<input type = "submit" input value="Change quantity" type="submit">
</form></body></html>
Copy linkTweet thisAlerts:
@GarySMay 18.2006 — Need to change your method to "post" ... or leave it as get and test for $_GET rather than $_POST
Copy linkTweet thisAlerts:
@wmwauthorMay 18.2006 — The code I reported did have POST as the form method. I removed the 4 lines of code starting at the third line and it works the right way. I'm wonderin' the line

$quantity = $_POST['quantity'];

might have something to do with that. Am I correct in saying that the way the form action is specified causes the execution go to the beginning of the code when the submit button is pressed? Thanks,
Copy linkTweet thisAlerts:
@GarySMay 19.2006 — Start with your last question first: the action causes the target page to be called (from the top); in this case, you want the form to call the page it is on.... so in the case the result is to run your code (which appears at the top of the page).

Now for some code:

[code=html]<?php
$quantity = 1;
if (isset($_POST) && $_POST!=null ){
$quantity = $_POST['quantity'];
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>

<form action="basket.php" method="post">
<input type = "TEXT" input name="quantity" value="<?php echo $quantity ?>">
<input type = "submit" input value="Change quantity" type="submit">
</form>

</body>
</html>[/code]


Have made a change or two:
[list]
  • [*]Have changed the $_POST test so that it checks that the $_POST is not empty. (Think $_POST may always be set?)

  • [*]Note the "post" in this line: <form action="basket.php" method="post"> . (Although you could do the whole thing with GET, this form "changes the world," so POST is the right choice.)

  • [/list]
    Copy linkTweet thisAlerts:
    @wmwauthorMay 19.2006 — Gary,

    One surprise came up when I implemented what I just learned about forms. In my actual code, another page passes a DB key to "basket.htm" as a global variable. (modified by global). In the first run the key is recognized and a nice table is presented. However in the second run after I pressed Submit, the global variable doesn't even exist. I'm going to use a super-global to see it would stayed recognized in basket.htm. Thanks,
    Copy linkTweet thisAlerts:
    @GarySMay 20.2006 — Couple of options for passing your DB key around:
    [list]
  • [*]Put it into a <input type="hidden" .... /> in your form (and test for it with $_POST)

  • [*]Put it into the action of your form as a query string action="basket.php?DBKEY=999" (... and test for it with $_GET)

  • [*]Put it into the session

  • [/list]
    Copy linkTweet thisAlerts:
    @wmwauthorMay 20.2006 — I used the session for passing data and it works just fine. Howerver there was complain of no DB coonection. I added mysql_connect() and mysql_select_db() without verifying db connection. I just don't know MySql good enough to tell this approach will exhaust DB connections. With my little 3 week hand on experience with PHP/MySQL, passing data across modules seems to be a weak area.
    ×

    Success!

    Help @wmw spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...