Forum Moderators: open

Message Too Old, No Replies

Posting to mysql database from javascript

         

malcolmcroucher

6:23 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



any one got any examples of posting to a mysql database from javascript ?

Fotiman

4:19 am on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You would not post to a mysql database from JavaScript. You could use AJAX to post the results to a URL, but you would need some sort of server side processing language (PHP, ASP, JSP, CGI, etc.) to take that data and send it to mysql.

malcolmcroucher

7:07 am on Sep 11, 2008 (gmt 0)

10+ Year Member



Thanks Im aware of that .

What ive done is created two drop downs which populate from a database but now im wondering how to put those selections into another database.

I have been using php and mysql and ajax

Fotiman

1:58 pm on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You would need to:
1. Decide what event will trigger the submission (click of a button, submit of a form, etc.)
2. Create the PHP page that will process the form data and populate the database.
3. Use AJAX to send your data. You might consider using a framework for this (like the Yahoo UI Library [developer.yahoo.com]'s Connection Manager) to handle the cross-browser issues.

That's basically all there is to it. Is there something in particular that you need help with?

malcolmcroucher

7:32 am on Sep 12, 2008 (gmt 0)

10+ Year Member



okay this is what i have done so far :

<?php
include "dbaconnect.php" ;
$i =0;
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Insert form</title>
<META NAME="keywords" CONTENT="Insert">
<META NAME="description" CONTENT="Insert">

<link rel="stylesheet" type="text/css" href="./css/home-font2.css"/>
<link rel="stylesheet" type="text/css" href="./css/home2.css" />
<link rel="stylesheet" type="text/css" href="./css/footer2.css" />
<script src="selectuser.js"></script>

</head>
<body>
<div id="banner">Insert</h1></div>
</div>
<div id="centercontent">
<h1> </h1>

<form action="insert2.php" method="post">
<table>

<tr><td>
seoid
</td><td>
<input type="text" name="pageid" />
</td></tr>
<tr><td>
keywordid

</td><td>
<input type="text" name="divid" />
</td></tr>
<tr><td>

Country

</td><td>


<?php

$sql1="SELECT * FROM country ORDER BY country";

$query1 = mysql_query($sql1);

echo '<select name="users" onchange="showUser(this.value)"';
while
($row1 = mysql_fetch_array($query1))
{

$i;

echo '<option value="',$row1['country'],'">',$row1['country'] ,'</option>';
$i++ ;

}
echo '</select>';

?>

</td></tr>
<tr><td>

</td></tr>
<tr><td>
cityid

</td><td>
<p>
<div id="txtHint"><b></b></div>
</p>

</td></tr>
<tr><td>

url

</td><td>
<input type="text" name="url" />
</td></tr>
<tr><td>
name
</td><td>

<?php
$sql="SELECT * FROM name";
$query = mysql_query($sql);

echo '<select name="cntry" style="width:200px" >';
while
($row = mysql_fetch_array($query))
{

$i;

echo '<option value="',$row['name'],'">',$row['name'] ,'</option>';
$i++ ;

}
echo '</select>';

?>

</td></tr>
</table>
<input type="submit" />

The following is the sql insert page after submit is pressed :

<?php
include "dbaconnect.php" ;

$sql="INSERT INTO test (continentid,countryid,cityid,url,name)
VALUES
('$_POST[continentid]','$_POST[users]','$_POST[myPrimarySelect]','$_POST[url]','$_POST[cntry]')
echo "$sql";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

so when i click submit the following is the result: (when i echo $sql )

INSERT INTO test (continentid,countryid,cityid,url,name)VALUES ('','America','New York','www.blah.com','south','hello','hiya','')1 record added

but when i look into the database i do not see the entry ? America and New York .

the ajax is working when i select x and a drop down of x derivatives occurs.

So my questions are the following :


1.Do i need more ajax to send the data

as per you instruction above ?

I dont know if this is a problem but i have php and ajax in the same form and would like to send both into one row in the database that is where my problem lies ?

The ajax is pretty identical to the w3schools ajax example in php .
if you want it i can post it but that is more for retrieving data not sending data ?

Im gonna work on it today , i will try find a solution before the us wakes up .

Regards

Malcolm

[edited by: Fotiman at 12:05 pm (utc) on Sep. 12, 2008]
[edit reason] Fixed bolds [/edit]