Jump to content

forum problem

- - - - -

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

#1
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Hello people! As i said, i am a new. Could u write me one simple code for forms? How then is it stored in dbase? For example:
<form>

Name:<input type="text"><br />

Surname:<input type="text"><br />

<option>

<select>A

<select>B

<select>c

</option>

<input type="submit" value="send">

</form>

So how i can store all that information that user entered to my dbase? Or simply to another php file?

Edited by Orjan, 19 October 2010 - 12:09 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Hello xle_camry, you will need to rewrite a bit of that to get it to work, I had added "name" attributes so they will work with PHP.
<form action="process.php" method="POST">
    Name:<input type="text" name="name"><br />
    Surname:<input type="text" name="surname"><br />
    <select name="options">
        <option>A</option>
        <option>B</option>
        <option>c</option>
    </select>
    <input type="submit" name="" value="send">
</form>

On process.php you can access the values with these:
$name = $_POST['name'];
$surname = $_POST['surname'];
$choice = $_POST['options']
Do you know how to work with MySQL?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Hello Nullw0rm, thanks for rewriting the code more clear. But your code also gives error. I have found how to work it without problem.
HTML CODE must be like this:
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Hello!</title>
</head>

<body>

<form name="form1" action="process.php" method="post">
Username: <input type="text" name="uname" /> <br />
Password: <input type="password" name="pass" /> <br />

Select type of weapon:<br />
<select name="weapon1">
<option value="sword">Sword</option>
<option value="rifle">Rifle</option>
<option value="knife">knife</option>
</select>
<input type="submit" value="Submit" name="submit1" />
</form>

</body>

</html>

PHP code must be like this:
<?php
$submit1 = $_POST['submit1'];
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$weapooon1 = $_POST['weapon1'];
echo $uname;
echo '<br />';
echo $weapooon1;
echo '<br />';
echo $pass;

?>
Yes , i can work with mysql, but as i said i am amateur. I want to learn deeply php. I even dont know how to directly insert this values into the database. I am more than happy if u would help me with my php. thaaaanks. :) let be friends.

Edited by Alexander, 19 October 2010 - 05:27 PM.
Merged posts


#4
noprobz09

noprobz09

    Newbie

  • Members
  • Pip
  • 8 posts
try this code,
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Hello!</title>
</head>

<body>

<form name="form1" action="process.php" method="post">
Username: <input type="text" name="uname" /> <br />
Password: <input type="password" name="pass" /> <br />

Select type of weapon:<br />
<select name="weapon1">
<option value="sword">Sword</option>
<option value="rifle">Rifle</option>
<option value="knife">knife</option>
</select>
<input type="submit" value="Submit" name="submit1" />
</form>

</body>

</html>

PHP code must be like this:
<?php
$connect=mysql_connect("localhost","root","your password");
if (!$connect)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("you database", $connect);

$submit1 = $_POST['submit1'];
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$weapooon1 = $_POST['weapon1'];
echo $uname;
echo '<br />';
echo $weapooon1;
echo '<br />';
echo $pass;
$sql="INSERT INTO tablename (field1,field2,field3)VALUES('$data1','$data2','$data3')";
$query=mysql_query($sql);
if($query)
{
echo'Data save successfully';
}
else
{
echo'Query Failed'.mysql_error();
}
?>
Hope It can be help!

#5
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Thanks so much. I'll try it. :)

#6
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
XLE another neat option is something like this:
<?php

if (isset($_POST['uname'])) {

     Processing code here!!

} else {

     echo '

<form name="form1" action="" method="post">

Username: <input type="text" name="uname" /> <br />

Password: <input type="password" name="pass" /> <br />


Select type of weapon:<br />

<select name="weapon1">

<option value="sword">Sword</option>

<option value="rifle">Rifle</option>

<option value="knife">knife</option>

</select>

<input type="submit" value="Submit" name="submit1" />

</form>';

}

?>
Its vital you use the ' and ' encasing the echo command if you use " and " PHP will think the " in this
me="form1
if the end of the echo command.
See how the code checks if the user has posted any data if the user has you can do something if the user hasn't you echo the form which just sends the user to the same page but this time with the post data.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).