We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,973 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Passing javascript variable into php

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.

5
Contributors
12
Replies
1 Day
Discussion Span
7 Months Ago
Last Updated
13
Views
TMD
Light Poster
25 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

@TMD

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

LastMitch
Industrious Poster
4,155 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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.

leviathan185
Junior Poster
157 posts since May 2009
Reputation Points: 24
Solved Threads: 19
Skill Endorsements: 0

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>
leviathan185
Junior Poster
157 posts since May 2009
Reputation Points: 24
Solved Threads: 19
Skill Endorsements: 0

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

Boby Smith
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
  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.

TMD
Light Poster
25 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

@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!

LastMitch
Industrious Poster
4,155 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

@ 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.

TMD
Light Poster
25 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

@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

LastMitch
Industrious Poster
4,155 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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

TMD
Light Poster
25 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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">

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1288 seconds using 2.74MB