4 Replies - 7481 Views - Last Post: 21 July 2012 - 03:51 PM Rate Topic: -----

#1 mshadow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-July 12

sending javascript variable to php on same page

Posted 19 July 2012 - 12:00 PM

<html>
<head>
<title>chat</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">



var win=window.frameElement.name;


</script>
</head>

</html>
<?php
 
 /*$mysqli = new mysqli("localhost", "root", "", "library");
  if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;}
 else
{
$mysqli->real_query("SELECT name, msg FROM message WHERE receiver = 'aditya' AND name = 'julu' OR receiver = 'julu' AND name = 'aditya' LIMIT 0 , 30;"); 
    $res=$mysqli->use_result();
    while($row=$res->fetch_assoc())
    {
       echo ">" .$row['name'].":". $row['msg'] . "\n <br>";

    }
}*/

?>




hey guys.
this might be easy for some of u , in the js section of the code i have a variable 'win'....now i want to send this win value from javascript to the php section....i tried the below ajax inside thejs section but it didn't work .........what am i doing wrong????
$.ajax({
   type: "POST",
   url: "id_test.php",
   data:{'variable': win},
   success: function(msg){
       
     
   }
 });



Is This A Good Question/Topic? 0
  • +

Replies To: sending javascript variable to php on same page

#2 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: sending javascript variable to php on same page

Posted 19 July 2012 - 12:36 PM

I don't think you can send a JS variable to PHP if the PHP code is (or was) in the same file. PHP is interpreted before a user receives the page. In other words, a page will never have PHP and Javascript functioning at the same time.

You could very well have an entire website with .php pages that look like
<?php

require 'content.php';
generate_page(1);

?>


because PHP is often used to generate dynamic HTML or even Javascript code.

You're better off sending the data to an external page.
Was This Post Helpful? 0
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: sending javascript variable to php on same page

Posted 19 July 2012 - 02:28 PM

In your id_test.php file, you can utilise that win variable by using this code:
$_POST['variable']


For example, your id_test.php file could have these contents:
<?php

echo $_POST['variable'];

?>


Which should print the variable on the screen.

Keep in mind that the jQuery $.ajax() function is intended for AJAX calls, which generally take place when a certain event fires, such as a mouseclick, or a select input changing value.
Was This Post Helpful? 0
  • +
  • -

#4 mshadow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-July 12

Re: sending javascript variable to php on same page

Posted 20 July 2012 - 12:43 AM

i used this in my php
$var="<script type='text/javascript'>document.write(win)</script>"
echo $var;
....


it displays var but the page keeps loading.....wat's d error????
Was This Post Helpful? 0
  • +
  • -

#5 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: sending javascript variable to php on same page

Posted 21 July 2012 - 03:51 PM

You can't access a Javascript variable directly in PHP, you have make a PHP request with data (such as your $.ajax() request above) and then use $_POST or $_GET in PHP to retrieve the value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1