homepage Welcome to WebmasterWorld Guest from 54.204.168.212
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Validate form data AND execute a php file with variables
Shepherd



 
Msg#: 4565653 posted 3:43 pm on Apr 17, 2013 (gmt 0)

I have an html form that is sending info to another site. I'm using javascript to validate the data inputted into the form prior to sending. I also want to execute a php file to save the data being sent to my own DB before the data is sent to the other site, any thought on how to do this?

current javascript code:
<script language="javascript" type="text/javascript">
function validateForm()
{
var x=document.forms["order"]["useraddress"].value;
if (x==null || x=="")
{
alert("An address is required.");
return false;
} else {
var y=document.forms["order"]["usercity"].value;
if (y==null || y=="")
{
alert("We need to know the State and City or County");
return false;
}
}
*** LOOKING FOR SOMETHING HERE TO CALL (POST) "TEST.PHP" WITH VARIABLES $useraddress=x and $usercity=y
}
</script>



Thanks!

 

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4565653 posted 5:46 pm on Apr 17, 2013 (gmt 0)

You could send an AJAX request to your own page, but be aware, if users have JavaScript disabled, then you won't receive that data, while the external site still will.

Readie

WebmasterWorld Senior Member



 
Msg#: 4565653 posted 3:11 pm on Apr 21, 2013 (gmt 0)

An alternative method is to have your form submit directly to your PHP script, and then have your PHP script send a cURL request to the third party site. You could then use the header() function to send the user to the third party site if necessary.

Shepherd



 
Msg#: 4565653 posted 2:31 pm on Apr 22, 2013 (gmt 0)

thanks for the suggestions. the following is what we decided to go with. seems to be working well, when the form is submitted the script validates the entries then saves the data to our database by loading a php script and then send the form data to the destination site.


<script language="javascript" type="text/javascript">
function validateForm()
{

var x=document.forms["order"]["useraddress"].value;
if (x==null || x=="")
{
alert("Address Required");
return false;
} else {
var y=document.forms["order"]["usercity"].value;
if (y==null || y=="")
{
alert("City Required");
return false;
}
}

if (document.getElementById("check1").checked)
{
selectedCost = document.getElementById("check1").value;
}
if (document.getElementById("check2").checked)
{
selectedCost = document.getElementById("check2").value;
}
if (document.getElementById("check3").checked)
{
selectedCost = document.getElementById("check3").value;
}


var xmlhttp;

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
// go ahead and return true, we submitted form

return true;
}
var url = "validate.php";
var params = "useraddress=" + encodeURIComponent(document.forms["order"]["useraddress"].value) + "&usercity=" + encodeURIComponent(document.forms["order"]["usercity"].value) + "&ItemCost="+selectedCost;
xmlhttp.open("POST","validate.php",true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.send(params);
}
</script>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved