问题描述
我已经看到很多问题把php值放入一个javascript变量中,但是我还没有看到任何将javascript变量放入php变量的问题。所以我的问题是有没有办法做到这一点,如果可以的话,你可以帮助我,或者至少指向正确的方向。
I have seen many questions as to putting a php value into a javascript variable however i have not seen any that put a javascript variable into a php variable. so my question is is there a way to do this, and if so could you help me or at least point me in the right direction.
到目前为止,我的代码是这样的:
my code so far is this:
<script>
function CheckForm()
{
var inputElement = document.getElementsByTagName("input");
if((inputElement[0].value !=="first" && inputElement[0].value !=="") &&
(inputElement[1].value !=="Initial" && inputElement[1].value !=="") &&
(inputElement[2].value !=="last" && inputElement[2].value !=="") &&
(inputElement[3].value !=="[email protected]" && inputElement[3].value !=="") &&
(inputElement[4].value !=="") &&
(inputElement[5].value !=="#" && inputElement[5].value !=="") &&
(inputElement[6].value !=="Lot" && inputElement[6].value !=="") &&
(inputElement[7].value !=="Evt. Name" && inputElement[7].value !=="") &&
(inputElement[8].value !=="xx/xx/xxxx" && inputElement[8].value !=="") &&
(inputElement[9].value !=="xx/xx/xxxx" && inputElement[9].value !=="") &&
(inputElement[10].value !=="xx:xx" && inputElement[10].value !=="") &&
(inputElement[11].value !=="") &&
(inputElement[12].value !=="xx:xx" && inputElement[12].value !=="") &&
(inputElement[13].value !=="") &&
(inputElement[14].value !=="") &&
(inputElement[15].value !=="") &&
(inputElement[16].value !=="x.xHours/Time Range" && inputElement[16].value !==""))
{
SendMail();
}
else
{
alert("One or more fields were not filled in correctly. Please try again.");
}
}
function GetBody()
{
var inputElement = document.getElementsByTagName("input");
var addInfo = document.getElementsByTagName("textarea");
var msg = "";
var line1= ("First Name: " + inputElement[0].value);
var line2= ("Middle Initial: " + inputElement[1].value);
var line3= ("Last Name: " + inputElement[2].value);
var line4= ("Email: " + inputElement[3].value);
//OTHER LINES OF MESSAGE HERE
msg = (line1 + "\n" + line2 + "\n" + line3 + "\n" + line4 + "\n" + line5 + "\n" + line6 + "\n" + line7 + "\n" + line8 + "\n" +
line9 + "\n" + line10 + "\n" + line11 + "\n" + line12 + "\n" + line13 + "\n" + line14 + "\n" + line15 + "\n" + line16 + "\n" +
line17);
return msg;
}
function SendMail()
{
var to = "[email protected]"
var from = document.getElementsByTagName("input")[3].value;
var subject = "Parking Lot Request";
var msg = GetBody();
<?php
$to = "GET to";
$subject = "GET subject";
$message = "GET msg";
$from = "GET from";
$headers = "From: " . $from;
mail($to, $subject, $message, $headers);
mail($from, $subject, $message, $headers);
echo "Request has been sent. Also a copy has been sent to you.";
?>
}
</script>
推荐答案
如果您试图在相同的页面,它不能完成。在页面到达浏览器之前,php会运行并呈现为HTML和Javascript,因此您的浏览器无法在同一页面上更改PHP变量。
If you're trying to do it all on the same page, it cannot be done. The php runs and renders into HTML and Javascript before the page ever gets to your browser, so your browser cannot possibly change a PHP variable on the same page.
现在,您肯定可以向您的服务器上的另一个甚至是同一个PHP页面发送一个AJAX请求,但是在您在浏览器中看到结果之前,该PHP也将运行并完成。任何你想让PHP做的事情都需要从Javascript获得一个变量,你必须使用AJAX。
Now, you can definitely send an AJAX request to another, or even the same, PHP page on your server, but that PHP will also run and finish before you ever see the result in your browser. Anything you want PHP to do that involves getting a variable from Javascript, you'll have to use AJAX.
这篇关于将一个java脚本变量的值插入到一个php变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!