0

Hello,
I want to ask how to retrieve the value of javascript variable in PHP, like this:

<script type="text/javascript"> 
   var data = "12345";
</script>


<?php
  $phpVar = $data;
?>

thanks

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

4
Contributors
7
Replies
9
Views
6 Years
Discussion Span
Last Post by happytogether
0

You need to understand the sequence of execution.

1) You php preprocess formats ouput for the client browser at server. Now php stops executing

2) That preprocessed code is sent to browser as html/javascript code. Where javacript code is executed, and html is rendered.

3) if you change any javascript variable, it is just changed at client browser and server(php) can not access it. IF you want to process some client action in php. the you must submit html form to server. then php can process submitted form and again give certain output.

0
<form method="post" action="whatever.php">
<input type="hidden" name="test" id="test">
</form>
<script type="text/javascript">
var data = "12345";
document.getElementById('test').value += data;
</script>
<?php
$phpVar = $_POST['test'];
?>

hope this work for you

0

if you want to send data back to the server without submitting the page, you can always use Asynchronous JavaScript and XML(AJAX). don't be fooled by the name, AJAX is not a new programming language, but a new way to use existing standards.

0

for you are great jquery, please help me to get value from javascript then put it into the php.
form of scripts like this:


page1.php

<script language="javascript">
  function selectMembers(idMember,Name,Address) {
    window.parent.selectMembers(idMember,Name,Address);
    window.parent.tb_remove();
   }
</script>

page2.php

<script language="javascript">
function selectMembers(idMember,Name,Address) {
  $('input[@name=idMember]').val(idMember);
  $('input[@name=Name]').val(Name);
  $('input[@name=Address]').val(Address);
  tb_remove();
}
</script>
<?php
  echo("<a href=\"list.php?width=850&height=400&TB_iframe=true&idMember=$idMember\" class=\"thickbox\"><img src=\"images/button_search.png\" border=\"0\" /></a>"); ?>
?>

---

The command does not work because the variable idMember is empty.

Thanks

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

0

try to change

<script language="javascript">
function selectMembers(idMember,Name,Address) {
  $('input[@name=idMember]').val(idMember);
  $('input[@name=Name]').val(Name);
  $('input[@name=Address]').val(Address);
  tb_remove();
}
</script>

into this

<script language="javascript">
function selectMembers(idMember,Name,Address) {
  $('input[name="idMember"]').val(idMember);
  $('input[name="Name"]').val(Name);
  $('input[name="Address"]').val(Address);
  tb_remove();
}
</script>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.