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

Recommended Answers

All 7 Replies

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.

<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

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.

thanks guys..

you can try jquery you can make youre script shorter..and make youre process faster.
it will really help if you are planning to use ajax
have a look at www.jquery.com

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

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