Jump to content

Simple Question - Data Input


BrandonSi

Recommended Posts

Do I need to use HTML to get user input from a browser? I'm writing a simple expense tracker and I want the data input to all be on one line, consisting of amount, category, description, payment method and date.

I've got the mysql db set up fine, and I put together code snippets to get a drop-down box to show the various categories available.. but when I try to create a simple text box for a user to input amount or description I get lost.. everything I've found on the web seems to use HTML forms.. I just wanna keep my straight .php file.

Here's my code for the drop-down box.

[code]<?
$dbh=mysql_connect ("localhost", "branthan_et", "enterprise") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("branthan_expensetracker");
$sqlOptions = "SELECT * from categories";
$resultOptions = mysql_query($sqlOptions);

echo '<select name="selectName">';
echo '<option value="">--Select--</option>';
while($opt = mysql_fetch_array($resultOptions))
{
echo '<option value="'.$opt['categories'].'">'.$opt['categories'].'</option>';
}
echo '</select>';
?>[/code]

Do I have to echo the HTML? If so could you share an example of how I would do that please? Basically I want to get all the input into variables and create a new record in the table. I can handle the sql code, but the simple stuff is really messing me up!
Link to comment
Share on other sites

Of course you have to use html. There is no such thing as a form in php.
[code]
<form action="handle.php" action="POST">
  <input type="text" name="amount">amount
  <input type="text" name="category">category
  <input type="text" name="description">description
  <input type="text" name="payment">payment
  <input type="text" name="amount">amount
  <input type="text" name="date">date
</form>
[/code]
That will post your data to [i]handle.php[/i] which will then be available in the $_POST array.
Link to comment
Share on other sites

[!--quoteo(post=386110:date=Jun 20 2006, 01:22 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 20 2006, 01:22 PM) [snapback]386110[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Of course you have to use html. There is no such thing as a form in php.
[code]
<form action="handle.php" action="POST">
  <input type="text" name="amount">amount
  <input type="text" name="category">category
  <input type="text" name="description">description
  <input type="text" name="payment">payment
  <input type="text" name="amount">amount
  <input type="text" name="date">date
</form>
[/code]
That will post your data to [i]handle.php[/i] which will then be available in the $_POST array.
[/quote]

Thanks.. That was kind of a dumb question, but I haven't ever worked with php.

Now how would I integrate a drop-down box that's populated from my mysql table into that one category field? I've got a php script to do it that executes fine from the browser as .php, is there a way to link a HTML form so that it gets its data from execution of that script?
Link to comment
Share on other sites

Like this:

[code]<form action="handle.php" action="POST">
  <input type="text" name="amount">amount
  <input type="text" name="category">category
  <input type="text" name="description">description

<?
$dbh=mysql_connect ("localhost", "branthan_et", "enterprise") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("branthan_expensetracker");
$sqlOptions = "SELECT * from categories";
$resultOptions = mysql_query($sqlOptions);

echo '<select name="selectName">';
echo '<option value="">--Select--</option>';
while($opt = mysql_fetch_array($resultOptions))
{
echo '<option value="'.$opt['categories'].'">'.$opt['categories'].'</option>';
}
echo '</select>';
?>

  <input type="text" name="payment">payment
  <input type="text" name="amount">amount
  <input type="text" name="date">date
</form>[/code]
Link to comment
Share on other sites

[!--quoteo(post=386119:date=Jun 20 2006, 01:52 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 20 2006, 01:52 PM) [snapback]386119[/snapback][/div][div class=\'quotemain\'][!--quotec--]
No....php is parsed on the server. Only the outputed html ends up on the client.
[/quote]

Thanks!

Does anyone see any formatting issues with that dropdown box php code? The output was fine when run as a .php but now I think the echo commands are screwed up.. maybe a quotation mark issue?

dropdown code
[code]
<?
$dbh=mysql_connect ("localhost", "branthan_et", "enterprise") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("branthan_expensetracker");
$sqlOptions = "SELECT * from categories";
$resultOptions = mysql_query($sqlOptions);

echo '<select name="selectName">';
echo '<option value="">--Select--</option>';
while($opt = mysql_fetch_array($resultOptions))
{
echo '<option value="'.$opt['categories'].'">'.$opt['categories'].'</option>';
}
echo '</select>';
?>[/code]


Output from browser
[code]
'; echo '--Select--'; while($opt = mysql_fetch_array($resultOptions)) { echo ''.$opt['categories'].''; } echo ''; ?> [/code]
Link to comment
Share on other sites

[!--quoteo(post=386212:date=Jun 20 2006, 07:30 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 20 2006, 07:30 PM) [snapback]386212[/snapback][/div][div class=\'quotemain\'][!--quotec--]
always save files with a .php exstention ok

example

test.php
[/quote]

God I feel dumb. I was saving it as an .html, since it had .html code in it. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

Thanks, it works perfectly now!
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.