Forum Moderators: open

Message Too Old, No Replies

Javascript Post to Mysql

         

malcolmcroucher

9:27 am on Sep 21, 2008 (gmt 0)

10+ Year Member



is it possible to post to a database from javascript ?

I figured out how to post but the problem is that it comes across in an array as

"usernamer=john"

so unless you split it up i doubt you would be able to do it ?

Regards

Malcolm

JAB Creations

12:37 pm on Sep 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something along these lines...

- John

Variable of HTTP queries/post parameters

function my_func1() {
var parameters = 'formis=' + encodeURI(document.getElementById('formis').value) +
'&reg_username=' + encodeURI(document.getElementById('reg_username').value) +
'&reg_username_base=' + username_base +
'&reg_password=' + encodeURI(document.getElementById('reg_password').value) +
'&reg_passwordconfirm=' + encodeURI(document.getElementById('reg_passwordconfirm').value) +
'&reg_email=' + encodeURI(document.getElementById('reg_email').value) +
'&reg_emailconfirm=' + encodeURI(document.getElementById('reg_emailconfirm').value);

alert(ajax_post(this.parentNode, 'test_ajax.php', parameters));
}

AJAX Function

function ajax_post(obj, url, parameters)
{
var http_request = false;
if (window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {http_request.overrideMimeType('text/html');} // set type accordingly to anticipated content type: http_request.overrideMimeType('text/xml');
}
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
http_request = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, false);
http_request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http_request.setRequestHeader('Content-length', parameters.length);
http_request.setRequestHeader('Connection', 'close');
http_request.send(parameters);
return http_request.responseText;
}

malcolmcroucher

6:07 pm on Sep 21, 2008 (gmt 0)

10+ Year Member



The other reason is that i am doing the following :

1. I have a form
2. With a drop where you select a country
3. From that selection another drop down appears for a city :
The city is displayed through :

<div id="txtHint">

4. then the user would fill in the rest of the form and press submit and the form would be inserted into a mysql database .

The problems :

1. Txthint is not allowing me to pass a variable
2. Post passes arrays which I dont need for a mysql post

malcolmcroucher

5:03 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



The problem is this :

I am doing onchange () in a drop down which calls the following function :

?php

$q=$_GET["q"];

$con = mysql_connect('localhost', 'malcolm', '');

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("tourz", $con);

$sql="SELECT * FROM city WHERE country = '".$q."'";

$result = mysql_query($sql);

echo "<SELECT NAME='myPrimarySelect' id='city' style='position: relative; width: 215px;' onChange=''>";

while($row = mysql_fetch_array($result))

{

print ("<option>" . $row['city'] . "</option>");

}

echo "</select>";

mysql_close($con);

?>

So for this function :

I choose America

I get a choice of
Washington , new york los angeles ect

However This data is displayed through

a <div id="txtHint">

Now i would like to take the data in the div id and process it to another form ?

That is where my problem lies.