could anyone tell me the mthod to use javascript/ajax to post to a sql database? or could someone point me to a tutorial?
javascript/ajax to mysql db
Collapse
X
-
Tags: None
-
-
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)....i t 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 formsComment
-
What kind of form elements do you have? Text boxes, hidden fields, password and textarea are easy:
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.Code:"fieldname=" + encodeURIComponent(document.getElementById("fieldID").value);Comment
-
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?Comment
-
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.Comment
-
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?
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; } 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>Comment
-
and maybe the mimetype, but i dont think you need it.
to make a request object you need this:
then your php will contain an update-query.Code: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(); } }
Code:$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);Comment
-
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:
assuming you already have a parameter, e.g. "file.php?var=1 ".Code:url += "&fieldname=" + encodeURIComponent(document.getElementById("fieldID").value);Comment
-
by the looks of what you're saying ciary i have to replace this with your first piece of coding.
is that correct?Code: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) {} } }Comment
Comment