Ok I have a program that uses HTML to get some values from a user using a form and then it is passed into a javascript function that does some calculations with these numbers and creates a new variable name.

What I need help with is that I want to email these new variables that were created in the javascript section using php. But I am unsure how to pass them into the php section of the code.

I am using a form to get the original information, along with a submit="post" button to send the information in the email.

Member Avatar for LastMitch

@TMD

Can you post the code? Javascript & PHP so we can see the problem!

You need to be passing the code into the javascript from a .php file

Lets say you have index.php and in the header you have your .js file referenced

When I need to pass a value from PHP to javascript I usually need it for a function for an onClick event or similar.

<?php
    $link = "<a href=\"javascript:function({$someVar});\">Link Text</a>";
?>

Or if you are inside a <script> tag

<script type="text/javascript">
    var jsVar = "<?php echo $phpvar; ?>";
    ...
    //do something with the variable    
</script>

the quotes are required either side of the php tags unless it is an integer.

I hope this helps, otherwise post your code we can take a look at it.

Sorry, just re-read your question.
So for your example you might do something like this;

<?php
    // this will usually go in your header somehere
    $user = $_POST['username']; // the username from the form posted to this page
    $pass = $_POST['password']; // the password from the form posted to this page
    // you might also want to validate the data and check it to make sure 
    // the $_POST has not been tampered with by the user before using it.
?>


<script type="text/javascript">
    // this must go somewhere after the obove php code. 
    // It can still go in the header if you wish.
    doSomething("<?php echo $user; ?>", "<?php echo $pass ?>");
</script>

It is redirection validation problem.Can you post the code? Javascript & PHP so we can see the problem!

  1. <html>
  2. <script language="php">
  3. $email = "personal email addres" . ",";
  4. $email .= $HTTP_POST_VARS['mailme'];
  5. $subject = "Attack";
  6. $data .= "Air 1: " . $HTTP_POST_VARS['air1'] . "n";
  7. $data .= "Air 2: " . $HTTP_POST_VARS['air2'] . "n";
  8. $data .= "Multiair: " . $HTTP_POST_VARS['multiair'] . "n";
  9. $data .= "Cc: " . $HTTP_POST_VARS['mailme'] . "nn";
  10. mail($email, $subject, $data, "From: myemailaddress");
  11. }
  12. </script>
  13. <script language="JavaScript" type="text/javascript">
  14. function randInt(min, max) {
  15. var div = (max - min) + 1;
  16. var randNum = Math.random();
  17. for (var i = 0; i <= div - 1; i++) {
  18. if (randNum >= i / div && randNum < (i+1) / div) {
  19. return i + min;
  20. }
  21. }
  22. return -1;
  23. }
  24. var air = document.getElementById('air').value;
  25. var air0 = document.getElementById('air0').value;
  26. var airroll = -1;
  27. var airroll0 = -1;
  28. for (var i = 0; i < parseInt(document.getElementById('rank').value); i++) {
  29. airroll = randInt(0, document.getElementById('air').value);
  30. if (airroll > greatestRoll) {
  31. greatestRoll = airroll;
  32. }
  33. }
  34. air1 = greatestRoll;
  35. greatestRoll = -1;
  36. for (var i = 0; i < parseInt(document.getElementById('rank').value); i++) {
  37. airroll0 = randInt(0, document.getElementById('air0').value);
  38. if (airroll0 > greatestRoll) {
  39. greatestRoll = airroll0;
  40. }
  41. }
  42. air2 = greatestRoll;
  43. greatestRoll = -1;
  44. multiair2 = ((air12)-(air22));
  45. </script>
  46. <body>
  47. <form action method="post">
  48. <font color="#FFC125"><strong>Air1 =</strong></font>
  49. <input type="text" size="3" name="air" id="air" value="0">
  50. <font color="#FFC125"><strong>Air2 =</strong></font>
  51. <input type="text" size="3" name="air0" id="air0" value="0">
  52. <input style="border:3px outset #FFC125; FONT-SIZE: 18pt; COLOR: #FFC125; BACKGROUND-COLOR: #000" onclick="updateResults()" type="submit" value="ATTACK!" name="sbutton" method="post" action="personalemailaddress"></p>
  53. <input name="submitted" type="hidden" value="1">
  54. <input type="hidden" size="10" value="0" name="points">
  55. </form>
  56. </html>

Here is more or less my code (I cut out other parts since it is doing the same thing. What I am trying to do is get the variables that I created in the javascript section and using php email it to a personal user, but I am unsure how to call the variables in the php section from the javascript section.

Member Avatar for LastMitch

@TMD

Here is more or less my code (I cut out other parts since it is doing the same thing. What I am trying to do is get the variables that I created in the javascript section and using php email it to a personal user, but I am unsure how to call the variables in the php section from the javascript section.

I'm not familiar with javascript! But can you show the variable so I can see the issue? Plus in the future when you paste your code please used "Code" on text editor so it will be highlight in blue!

Is there any reason that you implemented the functionality in javascript? You could probably do the same processing with php and just mail the values. But if you really want to use javascript for processing then you will have to use ajax approach - a javascript function that passes values to a php file that will send the mail.

http://www.w3schools.com/ajax/default.asp

@ Mitch

The variables that I am creating in the javascript code are "air1" "air2" and "multiair2" which I am trying to email using php. (which I noticed that I mislabeled in the php section, but it still isn't sent correctly.

@broj1

I'm not extremely familiar with php so which is why I am using javascrpit.

Member Avatar for LastMitch

@TMD

Do you have a database? If so what is the php file name?

If you don't have a database then I would suggest to used ajax.

In order to pass a varible you need to $_GET[] or $_POST[] function

I do not have a database, ok I'll use ajax then.

A note on using script tags: a <script> tag is, as far as I know, used for scripts that browser executes (javascript, vbscript...). For server side scripts use <? ?> tags which for PHP is:

<?php

// PHP code here

?>

or in your example:

<html>
<?php
$email = "personal email addres" . ",";
$email .= $HTTP_POST_VARS['mailme'];
$subject = "Attack";
$data .= "Air 1: " . $HTTP_POST_VARS['air1'] . "n";
$data .= "Air 2: " . $HTTP_POST_VARS['air2'] . "n";
$data .= "Multiair: " . $HTTP_POST_VARS['multiair'] . "n";
$data .= "Cc: " . $HTTP_POST_VARS['mailme'] . "nn";
mail($email, $subject, $data, "From: myemailaddress");
}
?>
<script type="text/javascript">
function randInt(min, max) {
...

The language attribute in script tag is deprecated in HTML 4.01. Use only <script type="text/javascript">

It's me again. Sory to post in pieces but I noticed another issue. Do not use $HTTP_POST_VARS array since it is deprecated and will be at some stage removed form PHP. Use $_POST array which is basically the same for your purpose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.