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.

Saving user input from HTML form to Database / PHP

  • 24-02-2015 02:25PM
    #1
    Registered Users, Registered Users 2 Posts: 2,609 ✭✭✭


    I have a HTML form that allows user input and it should Save to database when the button is clicked. It also should pick up the latitude and longitude from where it is located on the map (using Google Maps API V3).

    When the Save button is clicked, it does nothing - the form still remains with the text still displaying.

    The HTML form is:
    <form class="form-horizontal save-form" style="display: none">
    <h1>Add me!</h1>
    <fieldset>
    <div class="control-group">
    ....
    Form Contents Here
    ...
    </div>
    </div>
    <div class="form-actions">
    <input name="Save" type="submit" class="btn btn-primary" value="Save">
    </div>
    </fieldset>
    </form>

    The Javascript setting it to save is:
    //when user clicks on the "submit" button
    form.submit({point: point}, function (event) {
    //prevent the default form behavior (which would refresh the page)
    event.preventDefault();

    //put all form elements in a "data" object
    var data = {
    name: $("input[name=name]", this).val(),
    description: $("textarea[name=description]", this).val(),
    category: $("select[name=category]",this).val(),
    lat: event.data.point.overlay.getPosition().lat(),
    lon: event.data.point.overlay.getPosition().lng()
    };
    trace(data)

    //send the results to the PHP script that adds the point to the database
    $.post("adddata.php", data, tidy_maps.saveStopResponse, "json");

    //Erase the form and replace with new message
    infowindow.setContent('done')
    return false;

    The PHP is :
    <?php
    ini_set("display_errors",1);
    //database access info
    $db = new mysqli("localhost", "stephen", "pass1", "gmaps1");

    $statement = $db->stmt_init();

    //database insert statement
    $statement->prepare("INSERT INTO tidy_maps_test (lat, lon, name, description, category) VALUES (?, ?, ?, ?, ?)");

    //grab values from the url and add to database
    $statement->bind_param("ddsss", $_POST, $_POST, $_POST, $_POST, $_POST);
    $status = $statement->execute();
    //create a status message
    if ($status)
    {
    $message = array("message" => $_POST . " has been added!");
    }
    else
    {
    $message = array("error" => $db->error);
    }
    $statement->close();
    echo json_encode($message);
    ?>

    I'm not quiet sure what should go in here:
    VALUES (?, ?, ?, ?, ?)

    I have been following this tutorial: http://gis.yohman.com/up206b/tutorials/9-3/

    any other ideas of where im going wrong??
    Tagged:


Comments

Advertisement