0

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 !=="someone@example.com" && 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 = "someone@something.com"
    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>
Dylan
  • 47
  • 2
  • 8
  • unless there is something I'm missing, I don't want to use forms as i have already found that as a possible solution however the post function which i assume is why your pointing me there from what i understand requires HTML5. but i need this compatable with old browsers as well. probally something i should have mentioned in the original post. – Dylan Jul 31 '13 at 18:47
  • @Dylan — Nobody has mentioned anything that was introduced in HTML 5. – Quentin Jul 31 '13 at 18:48
  • @Quentin Fair enough. Deleting my comment. – GolezTrol Jul 31 '13 at 18:48
  • @Quentin someone had posted about a link to w3schools page on forms, which uses the html tag form with the attribute method="post", which from what i saw on the page on html forms is something new in html5. that person apparently removed that post before i finished posting that comment. – Dylan Jul 31 '13 at 19:01
  • @Dylan — Forms are [an HTML 2 thing](http://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8) – Quentin Jul 31 '13 at 21:27

1 Answers1

2

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.

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.

Samuel Reid
  • 1,756
  • 12
  • 22
  • thanks, that's the kind of answer i was looking for...off to research ajax...anyone else ever feel like smashing their head into a wall...or keyboard? – Dylan Jul 31 '13 at 18:55
  • If you're researching AJAX, I'd definitely look at the w3schools tutorial (despite what a lot of people on here say), and then look at jQuery's ajax functions. jQuery will simplify it for you once you actually know what's going on behind the scenes. – Samuel Reid Jul 31 '13 at 18:58
  • An Ajax post request on the jquery website is a neat little nice of code to start off with – Kevin Lynch Jul 31 '13 at 19:03