Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

html form

  • 15-01-2008 04:20PM
    #1
    Registered Users, Registered Users 2 Posts: 314 ✭✭


    I was just wondering if anyone could help me and tell me how to connect a html form to a sql database using php. I just require the code. Ive looked around and it isn't really helping me. I'm new to programming.


    <html>
    <head>
    <title>Booking Form</title>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859_1" />
    </head>
    <body>
    <h2>Booking Form</h2>
    <form action="http://google.ie"
    method="post" name="form1" id="form1">
    <table border="1">
    <tr>
    <td>First Name:</td>
    <td><input name="first name" id="first name" size="40" /></td>
    </tr>
    <tr>
    <td>Last Name:</td>
    <td><input name="last name" id="last name" size="40" /></td>
    </tr>
    <tr>
    <td>Address:</td>
    <td><input name="address" id="address" size="40" /></td>
    </tr>
    <tr>
    <td>Town:</td>
    <td><input name="town" id="town" size="40" /></td>
    </tr>

    <tr>
    <td>County:</td>
    <td><input name="county" id="county" size="40" /></td>
    </tr>


    <tr>
    <td>Phone Number:</td>
    <tr>
    <td>Home No:</td>
    <td><input name="home no" id="home no" size="4"> &nbsp <input name="home" id="home" size=7 /></td>
    </tr>

    <tr>
    <td>Mobile No:</td>
    <td><input name="mobile no" id="mobile no" size="4"> &nbsp <input name="mobile" id="mobile" size="7" /></td>
    </tr>
    <tr>
    <td>Email Address:</td>
    <td><input name="email" id="email" size="40" /></td>
    </tr>
    <tr>
    <td>Date Of Arrival:</td>
    <td>
    <select name="Date Of Arrival" id="Date Of Arrival">
    <OPTION value=1 selected>1</OPTION> <OPTION
    value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION
    value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION
    value=6>6</OPTION> <OPTION value=7>7</OPTION> <OPTION
    value=8>8</OPTION> <OPTION value=9>9</OPTION> <OPTION
    value=10>10</OPTION> <OPTION value=11>11</OPTION> <OPTION
    value=12>12</OPTION> <OPTION value=13>13</OPTION> <OPTION
    value=14>14</OPTION> <OPTION value=15>15</OPTION> <OPTION
    value=16>16</OPTION> <OPTION value=17>17</OPTION> <OPTION
    value=18>18</OPTION> <OPTION value=19>19</OPTION> <OPTION
    value=20>20</OPTION> <OPTION value=21>21</OPTION> <OPTION
    value=22>22</OPTION> <OPTION value=23>23</OPTION> <OPTION
    value=24>24</OPTION> <OPTION value=25>25</OPTION> <OPTION
    value=26>26</OPTION> <OPTION value=27>27</OPTION> <OPTION
    value=28>28</OPTION> <OPTION value=29>29</OPTION> <OPTION
    value=30>30</OPTION> <OPTION value=31>31</OPTION></SELECT>  
    <SELECT class=text
    onchange=setOutDayMonthIfBeforeInDayMonth(document.DatesForm)
    name=inmonth> <OPTION value=1 selected>Jan</OPTION> <OPTION
    value=2>Feb</OPTION> <OPTION value=3>Mar</OPTION> <OPTION
    value=4>Apr</OPTION> <OPTION value=5>May</OPTION> <OPTION
    value=6>Jun</OPTION> <OPTION value=7>Jul</OPTION> <OPTION
    value=8>Aug</OPTION> <OPTION value=9>Sep</OPTION> <OPTION
    value=10>Oct</OPTION> <OPTION value=11>Nov</OPTION> <OPTION
    value=12>Dec</OPTION></SELECT>    
    </select>
    </td>
    </tr>
    <tr>
    <td>No of Nights:</td>
    <td><input name="non" id="non" size="3" /></td>
    </tr>
    <tr>
    <td>Type Of Room Req'd:</td>
    <td>
    <select name="RoomType" id="RoomType">
    <option>Single</option>
    <option>Double</option>
    </select>
    </td>
    </tr>


    <tr>
    <td colspan="2"><br />Special Requirements:<br />
    <textarea name="comments" id="comments" rows="5" cols="50"></textarea></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><br />
    <input type="Submit" value="Submit" />
    <input type="Reset" value="reset" /><br /><br />
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    Thats basically the form, I havent a clue how to connect it to SQL using PHP. I know how to do the sql database, just connecting it with PHP is the problem. thanks for any help...


Comments

  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    $_POST; for life


    have a look at this
    http://www.tizag.com/phpT/forms.php

    you will need to setup connection variables at top though with ur sql details
    AND set up a connection.


  • Registered Users, Registered Users 2 Posts: 610 ✭✭✭nialo


    take a look at this post.. http://www.boards.ie/vbulletin/showthread.php?t=2055217567

    has all the code you need! this sits in the page your form posts to when you submit it.

    $_POST[‘username’] is the value submitted from the page.


  • Registered Users, Registered Users 2 Posts: 2,934 ✭✭✭egan007


    I have a tutorial on this on my blog but my site is offline at the mo so here's a paste...
    Moderator you can remove this if you want I know full answers are not the done thing but as I said if my site was working I'd be posting a link.....
    <?php
    
    //First thing we do is check the $_POST for the fields we are looking for.
    //The $_POST is basically an array so the notoation $_POST['name'] refers to the name elememt in the array.
    //If an element called name exists in the $_POST array then it will get assined to the variable $name.
    $name =$_POST['name'];
    $email =$_POST['email'];
    
    
    /*
    Below this comment I close my php sp that I can use regular html.
    The <form> tag below has two parameters.
    action - this tell it what to do when it gets a submit request i.e. Call this page index.php
    method - this tells it to put the data in the $_POST array
    */
    ?>
    
    
    <form action="form2sql.php" method="post">
    
    	<input type="text" id="name" name="name" value="">
    	
    	<input type="text" id="email" name="email" value="">
    	
    	<input type="submit" value="Submit">
    
    </form>
    
    
    <?php
    /*
    Ok I open my PHP Again
    Now I'll check if $name and $email got assigned anything.
    If they did then I'll connect to the database and put it in.
    */
    
    //This notation means -if the variable is not blank or empty
    if ($name != "" && $email != "")<br>
    {
    
    	//You can use this over and over again to connect to a database - just change the variables below to yours.
    	$username="user";
    	$password="password";
    	$database="databasename";
    		
    	$dbh=mysql_connect ("127.0.0.1", $username, $password) or die ('Error: Connection error, Can not connect to the database because: ' . mysql_error());
    	mysql_select_db ($database) or die( "Error: Selection error, Unable to select database $database" . mysql_error());
    	
    	
    	//Now build a query - I separate it out to it's easy to read if there is mistakes.
    	$query = "INSERT INTO `mytableName` 
    							( `name` , `email`)
                  VALUES 
    			  				('$name', '$email')
    			";
    							  
    	//Now submit the query.
    	mysql_query($query)
    
    	//Qed
    
    }
    
    ?>
    


    Here's what the code looks like without the comments save it in a file called.
    form2sql.php and rejoice

    <?php
    
    $name =$_POST['name'];
    $email =$_POST['email'];
    
    ?>
    
    
    <form action="form2sql.php" method="post">
    
    	<input type="text" id="name" name="name" value="">
    	
    	<input type="text" id="email" name="email" value="">
    	
    	<input type="submit" value="Submit">
    
    </form>
    
    
    <?php
    
    if ($name != "" && $email != "")
    {
    
    	$username="user";
    	$password="password";
    	$database="databasename";
    		
    	$dbh=mysql_connect ("127.0.0.1", $username, $password) or die ('Error: Connection error, Can not connect to the database because: ' . mysql_error());
    	mysql_select_db ($database) or die( "Error: Selection error, Unable to select database $database" . mysql_error());
    	
    	$query = "INSERT INTO `mytableName` 
    							( `name` , `email`)
                  VALUES 
    			  				('$name', '$email')
    			";
    
    	mysql_query($query)
    
    }
    
    ?>
    



    edit: If you are creating a booking form you need a way more than what's here.
    When you start taking in people's personal details you really need to know what you are doing.


Advertisement