could anyone tell me the mthod to use javascript/ajax to post to a sql database? or could someone point me to a tutorial?
61 3710
You can have a look at this.
You'll need some server-side processing to communicate with a db.
ive got the code to communicate with a database....thanks ill take a look
i've read over this site a few times....it doesn't have any details of what i need.
i basically need the details of a form to be duplicated on mouseover of the submit button once all fields have been filled in (i already wrote a code to check if form fields have been completed)....it takes the values from each field and sends it to a php page so that i can add it to a var and then enter it into a database.
the method you showed me is the right one but i don't have a clue where to start when it comes to forms as this has nothing on forms
What kind of form elements do you have? Text boxes, hidden fields, password and textarea are easy: - "fieldname=" + encodeURIComponent(document.getElementById("fieldID").value);
To get the values for other elements, e.g. select (single), get the value, and select (multiple), check the selected options. For check boxes and radio buttons, you need to find the checked buttons.
im using hidden fields to duplicate the value from visible fields into onChange....im looking for a way the values can go into php without having to refresh or go to a new page. if this cant be done can 1 form submit to two different pages?
There's no need to duplicate values. You can use the values from the visible fields. Show your Ajax code and I can suggest changes.
i dont have any....the only ajax examples i could find were to xml.....none to php so i don't even know where to start.
All you have to do is replace the XML file with a PHP one, e.g. the URL would be test.php instead of test.xml.
so if i took this code here which is an xml example....all i would have to do is change the file name at the end? -
<script type="text/javascript" language="javascript">
-
function makeRequest(url) {
-
var httpRequest;
-
-
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
-
httpRequest = new XMLHttpRequest();
-
if (httpRequest.overrideMimeType) {
-
httpRequest.overrideMimeType('text/xml');
-
// See note below about this line
-
}
-
}
-
else if (window.ActiveXObject) { // IE
-
try {
-
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
}
-
catch (e) {
-
try {
-
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
catch (e) {}
-
}
-
}
-
-
if (!httpRequest) {
-
alert('Giving up :( Cannot create an XMLHTTP instance');
-
return false;
-
}
-
httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
-
httpRequest.open('GET', url, true);
-
httpRequest.send('');
-
-
}
-
-
function alertContents(httpRequest) {
-
-
if (httpRequest.readyState == 4) {
-
if (httpRequest.status == 200) {
-
alert(httpRequest.responseText);
-
} else {
-
alert('There was a problem with the request.');
-
}
-
}
-
-
}
-
</script>
-
<span
-
style="cursor: pointer; text-decoration: underline"
-
onclick="makeRequest('test.xml')">
-
Make a request
-
</span>
-
and maybe the mimetype, but i dont think you need it.
to make a request object you need this: -
var httpRequest;
-
-
try{
-
httpRequest = new ActiveXObject("MSXML2.XMLHTTP");
-
}catch(exception1){
-
try{
-
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
}catch(exception2){
-
httpRequest = false;
-
}
-
-
if(!httpRequest && window.XMLHttpRequest){
-
httpRequest = new XMLHttpRequest();
-
}
-
}
-
then your php will contain an update-query. -
$id = mssql_connect('***.***.***.***,1433','mssqluser','passwd');
-
mssql_select_db("myDB",$id);
-
-
result = mssql_query("UPDATE table_name SET column_name = ".$_GET['value']." WHERE id = ".$_GET['id']);
-
-
mssql_close($id);
-
That's what you would have to do to make a request to a PHP file, but in that PHP file, it should contain the code to deal with the passed values. The change you have to make to pass form values would be in makeRequest before you open: - url += "&fieldname=" + encodeURIComponent(document.getElementById("fieldID").value);
assuming you already have a parameter, e.g. "file.php?var=1".
by the looks of what you're saying ciary i have to replace this with your first piece of coding. - if (window.XMLHttpRequest) { // Mozilla, Safari, ...
-
httpRequest = new XMLHttpRequest();
-
if (httpRequest.overrideMimeType) {
-
httpRequest.overrideMimeType('text/xml');
-
// See note below about this line
-
}
-
}
-
else if (window.ActiveXObject) { // IE
-
try {
-
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
}
-
catch (e) {
-
try {
-
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
catch (e) {}
-
}
-
}
is that correct?
There's no need for that. The code you already have is actually better.
ok just looking over a tutorial and this code do i need to do changes to the function alertContents(httpRequest) or is that fine as well?
Well, you would need to make changes to that unless you only wish to alert the response.
What exactly are you attempting to do by submitting the form? Do you want to give some notification when it's complete?
no, it's fine. but if you only have an update query, you won't have a responsetext so it will be unnecessary.
[EDIT] unless you have a echo something in php like acoder said.
if you still want to alert something or hide a loader when the update is done, you can use that instead.
what it is.....its a paypal form but i want to save the form details into a database before it goes to paypal then once the paypal purchase is complete the user would be redirected to a page to confirm their email address so a confimation email is sent to them with a username and password....i've had to go about it this way because paypal do not allow fo extra confirmation emails and refuse to add that option on any account, at 1st i tried to use curl but paypal doesn't allow that....so the only way was with javascript/ajax so it does it all on the same page then the user can just click submit and it will go straight to paypal
What information do you want to save in the database? Post the form HTML code.
and which of these fields do you want to store in the database?
all fields except the hidden fields need to be stored as its to build a customer database
For the fields, either add an ID so you can use document.getElementById() to access them or use: - document.getElementById("form").elements[name-of-field].value
To make a POST request (use the send() method), construct the field-value parameters, e.g. - var urlparams = "first_name=" + encodeURIComponent(document.getElementById("first_name").value);
-
urlparams += "&last_name=" + encodeURIComponent(document.getElementById("first_name").value);
-
// and so on...
@acoder
you have lost me on here....im a newbie with ajax, i dont want any alerts as long as i test it and see the values going into the database that's fine.
"file.php?var=1" <---where do i add this in?
That was with a GET request. Are you using GET or POST in PHP?
ok i understand on how to select the fields i need but i don't understand where to put that in the code
paypal uses post...can't use get unfortunately
It doesn't matter if Paypal uses Post. I'm asking about your PHP code.
Let's assume POST. Then keep the URL free of parameters and send the parameters via the send method: - httpRequest.send(urlparams);
@anfetienne
Before you open the request (before line 28 in post #11), then change 'GET' to 'POST': - httpRequest.open('POST',url,true);
@acoder
now i understand what you done here....i think i get all of it almost......last thing is where do i place this in my ajax code?
Thanks in advance everyone....been a great help!!!!
if you did this with GET you would have done this -
var url = "file.php"
-
url += "?firstname="+document.getElementById("form").elements['first_name'].value
-
url += "&lastname="+document.getElementById("form").elements['last_name'].value
-
url += "&city="+document.getElementById("form").elements['city'].value
-
-
//... you have to do all your fields like this
-
httpRequest.open('GET',url,true);
-
ok final question lol...thanks again for the help though......say i put all this coding in a file index.php and i wanted it to do everything within this file would i call the file at the end index.php? and would it be best to use the if and else statement in php to run the sql once ajax has done its part?
It might be easier to keep it in separate files, but if you do want it in one file, then yes, you would need to use if-else to ensure that the correct part is run, e.g. by checking the existence of one of the POST-ed values.
ok got it....thanks for all the help im going to be testing tomorrow so ill let you know the results then
i forgot to ask....what is it i put to run onmouseover or onsubmit etc?
Do you mean your validation function? It should run onsubmit, but if you're using Ajax to submit the form, you could just call the validation function within the makeRequest() function.
ok ive tested the code and it works the only problem i'm having is that it won't run the code without opening another page. So far i have only got it to work when the form submission looks like this
<form action="" method="post" target="paypal" id="form">
this opens a new window and it does as it should then but i can't get it to run in the same window
i think this is where if and if else would come in actually....i know this should be in the php forum but as you mentioned it how do you do a if statement on $_POST
You can use isset to check if a variable is set.
@anfetienne
Can you post the latest version of your code.
this is my code -
<script type="text/javascript" language="javascript">
-
function makeRequest(url) {
-
var httpRequest;
-
-
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
-
httpRequest = new XMLHttpRequest();
-
if (httpRequest.overrideMimeType) {
-
httpRequest.overrideMimeType('text/xml');
-
// See note below about this line
-
}
-
}
-
else if (window.ActiveXObject) { // IE
-
try {
-
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
}
-
catch (e) {
-
try {
-
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
catch (e) {}
-
}
-
}
-
-
if (!httpRequest) {
-
alert('Giving up :( Cannot create an XMLHTTP instance');
-
return false;
-
}
-
-
//enter var details for fields here
-
var urlparams = "first_name=" + encodeURIComponent(document.getElementById("first_name").value);
-
urlparams += "&last_name=" + encodeURIComponent(document.getElementById("last_name").value);
-
urlparams += "&address1=" + encodeURIComponent(document.getElementById("address1").value);
-
urlparams += "&address2=" + encodeURIComponent(document.getElementById("faddress2").value);
-
urlparams += "&city=" + encodeURIComponent(document.getElementById("city").value);
-
urlparams += "&state=" + encodeURIComponent(document.getElementById("state").value);
-
urlparams += "&zip=" + encodeURIComponent(document.getElementById("zip").value);
-
urlparams += "&os0=" + encodeURIComponent(document.getElementById("os0").value);
-
urlparams += "&email=" + encodeURIComponent(document.getElementById("email").value);
-
-
-
httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
-
httpRequest.open('POST', url, true);
-
httpRequest.send('urlparams');
-
-
}
-
-
function alertContents(httpRequest) {
-
-
if (httpRequest.readyState == 4) {
-
if (httpRequest.status == 200) {
-
alert(httpRequest.responseText);
-
} else {
-
alert('There was a problem with the request.');
-
}
-
}
-
-
}
-
</script>
-
and this is the form....i have put the paypal link as a php comment so you can see the address the form submits to
The reason it opens a new window is the target attribute. If you remove that, it won't open a new window. However, having said that, onsubmit should be added to the form tag, not the submit button. Remind me, did you want to prevent a page unload completely or did you want to make one submit (with Ajax) and then another page unload/reload one later with Paypal?
Note: 'urlparams' should be just urlparams without the quotes.
i just want to save the form data to a db whilst paypal opens in a new window....nothing to do with paypal is going to change, just need to be able to get it to send data to db in the same page or in a seperate page.
change httpRequest.send('urlparams'); to httpRequest.send(urlparams);
I just noticed that when you call makeRequest(), you're passing an empty string instead of the URL.
ok so i have to take away the quotes on urlparams and makeRequest() should be makeRequest(url)?
Yes and no. Pass the actual URL as a string, e.g. Sign in to post your reply or Sign up for a free account.
Similar topics
by: KDCinfo |
last post by:
Although I'm making an ajax call, this is really a javascript question
(although it could be even more of an HTML or DOM question... not
exactly sure)
I'm doing an ajax call to a remote php...
|
by: Free Ebooks |
last post by:
81 AJAX and 24 JavaScript Ebooks
Here are some of the AJAX topics and areas covered by these ebooks:
Rails and AJAX
Building Ajax Web Applications
Creating Ajax Web Pages
Ajax Patterns
Ajax...
|
by: SM |
last post by:
Hello,
Im trying to access elements in my XML file using the JavaScript DOM
but i'm not sure how. I use AJAX to access the XML and then use the
responseXML property to access the XML file data.
I...
|
by: Nathan Sokalski |
last post by:
I am moving my website from my machine to my webhost, and need some help
with what extra files I need to include due to the fact that I used AJAX in
my site. Everything on the site is obviously...
|
by: paulie |
last post by:
Hi,
I have been experiencing an issue when trying to use AJAX to reload a DIV area
using a timer of 2000ms, which contains a html page with another DIV and javascript.
Scenario
-------------...
|
by: jmohan |
last post by:
Dear Sir/Madam,
I develop a website in asp.net with c#. And also, I develop a toolbar in the toolbar studio separately.
Aim of the Website: Enabling the visitors to become a member of our...
|
by: jarremw |
last post by:
hello all, what i have is a modal popup control extender, i have an ajax script that saves the value of the two textboxes that are in the popup, what i am needing is a way to insert those values into...
|
by: Patient Guy |
last post by:
Which is the better approach in working with Javascript?
1. Server side processing: Web server gets form input, runs it into the
Javascript module, and PHP collects the output for document prep....
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |