Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Saving user input from HTML form to Database / PHP

  • 24-02-2015 1:25pm
    #1
    Registered Users, Registered Users 2 Posts: 2,608 ✭✭✭


    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