I wonder whether someone may be able to help me please.
I’m currently working on a website that through a User Input Form, XML and PHP, users will add and save google map information to my SQL database.
I’ve successfully been using one of the examples on the Google site which explains how to use PHP and XML to save user defined information to the database.
The example use the $GET method, but from the information I’ve been reading on the web, the $POST option does seem the better method to use.
However when I change my coding to the $POST method I can create a record on the database but the values keyed into the form are not copied scross to the database. I know that I’m perhaps making a really stupid beginners mistake, but I just wondered whether someone may be able to tell me what I need to do to get it to work.
I’ve included cut down versions of the files below.
Many thanks
Chris
HTML Form Submit Button
function savedata() {
var locationname = document.getElementById("locationname").value;
var returnedaddress = document.getElementById("returnedaddress").value;
var url = "phpfilename.php?locationname=" + locationname + "&returnedaddress=" + returnedaddress;
downloadUrl(url, function(data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
}
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);
}
};
request.open('POST', url, true);
request.send(null);
}
function doNothing() {}
</script>
HTML Form
<body>
<label><br />
</label>
<form name="searchforlocationformgeocode" id="searchforlocationformgeocode">
<div>
<label for="address">
<div align="center">
<p>Location Name</p>
<p>
<input name="locationname" type="text" id="locationname" size="80" />
</p>
</div>
<p align="left"><label>Returned Address</label> </p>
<div align="left">
<input name="returnedaddress" type="text" id="returnedaddress" size="100" />
</div>
PHP File
<?php
require("phpfilename.php");
// Gets data from URL parameters
$locationname = $_POST['locationname'];
$address = $_POST['returnedaddress'];
// Opens a connection to a MySQL server
$connection = mysql_connect ("xxxxxxxx", $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Insert new row with user data
$query = sprintf("INSERT INTO tablename " .
" (locationname, address) " .
" VALUES ('%s', '%s', '%s' );",
mysql_real_escape_string($locationname),
mysql_real_escape_string($address));
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
You are passing the values using a query string, to get them you need to use $_GET.
To use $_POST, instead of adding the encoded name/value pairs to the end of the URL, send them via the request.send() method: