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>
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’);”);
?>
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.