/    Sign up×
Community /Pin to ProfileBookmark

Using javascript to execute SQL statements via PHP

OKay, I’ve spent more than a few hours searching the net looking for a simple way to do this. I’ll try to be as clear as I can…

I have a test.php page sitting at mydomain.com/test.php.

In this php page (test.php) I have a call to my database that will create a new table and enter some data into that table.

If I type in my browser mydomain.com/test.php the php script runs and updates my database, so I know my php script works.

Now in an HTML file called index.html I have some javascript functions do do a few things. When I click a button it validates a few fields, and then refreshes the page.

NOW, what I want to do is right before the page gets refreshed, I want to execute that test.php file. (Eventually using the data the user filled in to update some tables, but I just want to get the php file to run first)

Here’s my HTML and js code….

[CODE]<html>

<body bgcolor=”000000″>

<script type=’text/javascript’>
function formValidator(){
// Make quick references to our fields
var teamname = document.getElementById(‘teamname’);
var captainname = document.getElementById(‘captainname’);
var mem1name = document.getElementById(‘mem1name’);
var mem2name = document.getElementById(‘mem2name’);
var mem3name = document.getElementById(‘mem3name’);
var username = document.getElementById(‘username’);
var email = document.getElementById(’email’);

// Check each input in the order that it appears in the form!
if(isAlphanumeric(teamname, “Only Letters and Numbers in Team Name!”)){
if(isAlphabet(captainname, “Please enter only letters for Captain’s name”)){
if(isAlphabet(mem1name, “Please enter only letters for Member 1 name”)){
if(isAlphabet(mem2name, “Please enter only letters for Member 2 name”)){
if(isAlphabet(mem3name, “Please enter only letters for Member 3 name”)){

if(emailValidator(email, “Please enter a valid email address”)){

return true;
}

}
}
}
}
}

return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert(“Please enter between ” +min+ ” and ” +max+ ” characters”);
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == “Please Choose”){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function emailValidator(elem, helperMsg){
var emailExp = /^[w-.+]+@[a-zA-Z0-9.-]+.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function toggle_username(userid) {
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http = new ActiveXObject(“Microsoft.XMLHTTP”);
}
handle = document.getElementById(userid);
var url = ‘ajx.php?’;
if(handle.value.length > 0) {
var fullurl = url + ‘do=check_username_exists&username=’ + encodeURIComponent(handle.value);
http.open(“GET”, fullurl, true);
http.send(null);
http.onreadystatechange = statechange_username;
}else{
document.getElementById(‘username_exists’).innerHTML = ”;
}
}

function statechange_username() {
if (http.readyState == 4) {
var xmlObj = http.responseXML;
var html = xmlObj.getElementsByTagName(‘result’).item(0).firstChild.data;
document.getElementById(‘username_exists’).innerHTML = html;
}
}

</script>

<form onsubmit=’return formValidator()’ >
Team Name: <br><input type=’text’ id=’teamname’ />
<br>
Username: <br>
<input id=”username” type=”text” name=”username” onchange=”toggle_username(‘username’)” /><br />
<div id=”username_exists” style=”font-size: 11px;font-weight: bold;color:#FF3300″> </div>
Captain Name: <br><input type=’text’ id=’captainname’ /> <br />
Member 1: <br><input type=’text’ id=’mem1name’ /><br />
Member 2: <br><input type=’text’ id=’mem2name’ /><br />
Member 3: <br><input type=’text’ id=’mem3name’ /><br /><br>
<br><br>
Email: <br><input type=’text’ id=’email’ /> <br />
<input type=’submit’ value=’Submit Team Info’ />
</form>
<br>
</FONT> </td>
<td align=”left”>other side of table</td>
</tr>
</table>
</center>

</body>
</html>[/CODE]

Here’s the PHP code (should work correctly, I changed the user, pass and host)………

[CODE]<?php
mysql_connect (‘localhostname’, ‘username’, ‘password’);
mysql_select_db(‘mydbname’);

mysql_query(“CREATE TABLE `users` (
`username` varchar(20) NOT NULL
) TYPE=MyISAM;”);

mysql_query(“INSERT INTO `users` VALUES(‘username’);”);
mysql_query(“INSERT INTO `users` VALUES(‘example’);”);

?> [/CODE]

Can someone please help me?

All I want is for someone to show me the simplest way to run a php file after they click that submit button.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@steveaNov 13.2008 — The first problem I see is that your form tag doesn't have an action set, so it is posting to the current page, not your php script. If I'm understanding you correctly you want the form to post to your php file and use the data passed to it from the form to insert into the database. If so you're going to need to modify your php script to actually make use of the data it's receiving.

The simple answer to your question is that you have to change your form tag to read:

<i>
</i>&lt;form onsubmit='return formValidator()' action='test.php' method='POST' &gt;


The method='POST' isn't strictly necessary though. You will be better off posting into the PHP section for the rest of your question though, you will get more responses there regarding how to use the data passed to test.php.

Hope that helps.
Copy linkTweet thisAlerts:
@MrNobodyNov 13.2008 — Can someone please help me?

All I want is for someone to show me the simplest way to run a php file after they click that submit button.[/QUOTE]

The simplest way is to point your FORM at the test.php page and have your test.php page redirect back to index.html (or whatever). However, you can also keep your index.html page as is and use AJAX to call your test.php page directly from JavaScript. That's not the simple method -- but it is clean.
×

Success!

Help @dutchman1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...