Javascript not passing variable to PHP with HTML form

I have contact form, that uses Javascript and PHP. My problem is that the javascript does not pass its variables to the PHP script. I have discovered the following: The variables does get set in the javascript, as it passes the variable check inside the javascript and by using alerts showing the variable. As it travels to the PHP script, the variables are no longer there. I have tried to comment out the check in the PHP script, then it succesfully send a mail, but the mail was empty apart from the static content from the PHP script. I have tried various different methods, including passing the variables directly from the http.send() function. Here is the relevant code (These are all 3 different files on the web server): HTML: div class="contact_form">

Get in touch

Javascript: var http = createRequestObject(); var areal = Math.random() + ""; var real = areal.substring(2,6); function createRequestObject() { var xmlhttp; try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} catch(f) { xmlhttp=null; } } if(!xmlhttp&&typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } return xmlhttp; } function sendRequest() { var rnd = Math.random(); var name = escape(document.getElementById("name").value); var email = escape(document.getElementById("email").value); var subject = escape(document.getElementById("subject").value); var body = escape(document.getElementById("body").value); try{'POST','pform.php'); http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); http.send('name='+name+'&email='+email+'&subject='+subject+'&body='+body+'&rnd='+rnd); http.onreadystatechange = handleResponse; } catch(e){} finally{} } function check_values() { var valid = ''; var name = document.getElementById("name").value; var email = document.getElementById("email").value; var subject = document.getElementById("subject").value; var body = document.getElementById("body").value; if(trim(name) == "" || trim(email) == "" || trim(subject) == "" || trim(body) == "") { alert("Please complete all fields"); } else { if(isEmail(email)) { document.getElementById("submit").disabled=true; document.getElementById("submit").value='Please Wait..'; sendRequest(); } else { alert("Email appears to be invalid\nPlease check and try again"); document.getElementById("email").focus(); document.getElementById("email").select(); } } } function handleResponse() { try{ if((http.readyState == 4)&&(http.status == 200)){ var response = http.responseText; document.getElementById("confirmation").innerHTML = response; document.getElementById("confirmation").style.display =""; } } catch(e){} finally{} } function isUndefined(a) { return typeof a == 'undefined'; } function trim(a) { return a.replace(/^s*(S*(s+S+)*)s*$/, "$1"); } function isEmail(a) { return (a.indexOf(".") > 0) && (a.indexOf("@") > 0); } PHP: $confirmation"; die(); ?>
On the PHP script, you have to retrieve you data from $_POST as your AJAX call is POSTING data. in your PHP you are using directly your vars. who has initialized them .?

以上就是Javascript not passing variable to PHP with HTML form的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准