Spedwards
Spedwards

Reputation: 4492

PHP form submit to sql database

I'm currently creating a custom 'survey'-like form but since I'm new to php, I am not entirely sure how to submit the entries to a database (and make a fieldset required).

I currently have 3 fieldsets, lets call them who, when and what. Who is a simple select field so no problem. When however is a group of radios but two of those have text fields associated that need to be filled if the radio was selected. What is just a textarea.

I understand this much:

With the database, how do I submit this data? Especially the second fieldset.

Current Form:

    <form action="heroes.php" method="post">
        <fieldset>
            <h2 class="req">Which Hero?</h2>
            <span class="help">We need to know what hero says what line.</span>
            <label><span class="title">Hero: </span>
                <select>
                    <?php include 'files/hero-select.php'; ?>
                </select>
            </label>
        </fieldset>

        <fieldset id="when">
            <h2 class="req">When?</h2>
            <span class="help">When is it said? Who is it said to?</span>
            <label>To Boss: <input name="when" type="radio" value="boss" /> <input type="text" placeholder="Boss Name" id="bname" size="10" /></label>
            <label>To Hero: <input name="when" type="radio" value="hero" /> <input type="text" placeholder="Hero Name" id="hname" size="10" /></label>
            <label>Low Health: <input name="when" type="radio" value="lowhp" /></label>
            <label>Defeat: <input name="when" type="radio" value="defeat" /></label>
            <label>Boss Defeat: <input name="when" type="radio" value="bossd" /></label>
            <label>Mob Defeat: <input name="when" type="radio" value="mob" /></label>
            <label>Emote: <input name="when" type="radio" value="emote" /></label>
            <label>Item Pickup: <input name="when" type="radio" value="item" /></label>
        </fieldset>

        <fieldset>
            <h2 class="req">Quote</h2>
            <span class="help">What was said.</span>
            <textarea id="quote"></textarea>
        </fieldset>

        <fieldset>
            <input type="submit" value="Submit" /><input type="reset" value="Clear" />
        </fieldset>
    </form>

hero-select.php: Black Panther

                        <option value="Black Widow">
                            Black Widow
                        </option>

                        <option value="Cable">
                            Cable
                        </option>

                        <option value="Captain America">
                            Captain America
                        </option>

                        <option value="Colossus">
                            Colossus
                        </option>

                        <option value="Cyclops">
                            Cyclops
                        </option>

                        <option value="Daredevil">
                            Daredevil
                        </option>

                        <option value="Deadpool">
                            Deadpool
                        </option>

                        <option value="Emma Frost">
                            Emma Frost
                        </option>

                        <option value="Gambit">
                            Gambit
                        </option>

                        <option value="Ghost Rider">
                            Ghost Rider
                        </option>

                        <option value="Hawkeye">
                            Hawkeye
                        </option>

                        <option value="Hulk">
                            Hulk
                        </option>

                        <option value="Human Torch">
                            Human Torch
                        </option>

                        <option value="Iron Man">
                            Iron Man
                        </option>

                        <option value="Jean Grey">
                            Jean Grey
                        </option>

                        <option value="Loki">
                            Loki
                        </option>

                        <option value="Luke Cage">
                            Luke Cage
                        </option>

                        <option value="Moon Knight">
                            Moon Knight
                        </option>

                        <option value="Ms Marvel">
                            Ms Marvel
                        </option>

                        <option value="Nightcrawler">
                            Nightcrawler
                        </option>

                        <option value="Punisher">
                            Punisher
                        </option>

                        <option value="Rocket Raccoon">
                            Rocket Raccoon
                        </option>

                        <option value="Scarlet Witch">
                            Scarlet Witch
                        </option>

                        <option value="Spider-Man">
                            Spider-Man
                        </option>

                        <option value="Squirrel Girl">
                            Squirrel Girl
                        </option>

                        <option value="Storm">
                            Storm
                        </option>

                        <option value="Thing">
                            Thing
                        </option>

                        <option value="Thor">
                            Thor
                        </option>

                        <option value="Wolverine">
                            Wolverine
                        </option>

Upvotes: 0

Views: 889

Answers (2)

Jan-Willem de Boer
Jan-Willem de Boer

Reputation: 797

I'm gonna do a short example, with different code (but it works the same).

<form method="POST" action="add.php">
    Name: <input type="text" name="name">
    Phone: <input type="text" name="phone">
</form>

method="POST" means that it Posts all the info to the action, the action is set to add.php so it posts all the info to add.php.

As you can see every input above has a different name="", what the name property does is: It sends every value together with a name, this means that the phone input gets send as Phone = "123456" and the name value Name = "2324234".

The add.php code

<?php
if ($_SERVER['REQUEST_METHOD']) == 'POST') {
  $name = $_POST['name'];
  $phone = $_POST['phone'];

  $sql = 'INSERT INTO user '.
   '(name,phone) '.
   'VALUES ( "$name", "$phone"NOW())';

    mysql_select_db('test_db');
    $retval = mysql_query( $sql, $conn );
 }

First it check if the request is a post, if it is it turns the value's it get send into variables and then it inserts the values into the database.

Upvotes: 2

KoalaBear
KoalaBear

Reputation: 2948

You can use jQuery with the .post method:

https://api.jquery.com/jQuery.post/

$.post("submittodatabase.php", $( "#yourformid" ).serialize() );

Upvotes: 0

Related Questions