简体   繁体   中英

PHP/MySQL: Saving user input to Database

I have a page which brings up a map where the user can add a marker and brings up a few input boxes for them to add info (name, description, etc.). This info is then to be sent to a database where it can be recalled and displayed where necessary.

When SAVE is clicked, the box should close and display a message ("Done")

The JS function which sends it to database:

    //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 'adddata.php':

<?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['lat'], &$_POST['lon'],     &$_POST['name'], &$_POST['description'], &$_POST['category']);
$status = $statement->execute();

//create a status message
if ($status)
{
    $message = array("message" => $_POST['name'] . " has been added!");
}
else
{
    $message = array("error" => $db->error);
}

$statement->close();

echo json_encode($message);
?>

When the SAVE button is clicked, nothing happens, the form stays open with the text input and does not save to DB. I ran the 'adddata.php' on my browser to see if it is connected to the database and it is.

I've been using this tutorial as a guide : http://gis.yohman.com/up206b/tutorials/9-2/


I've tried changing the button to an input but still no luck. My 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>

Your code (from tutorial) uses function 'trace'

trace(data);

but this function is not defined. In tutorial demo this function defined as:

function trace(message)
{
  if (typeof console != 'undefined')
  {
      console.log(message);
  }
}

Try to add this code in your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM