Link to home
Create AccountLog in
Avatar of evault
evaultFlag for United States of America

asked on

how do I pass a java script variable back to the PHP Server

I am trying to edit a user in a database. I am using a Java Script to select the user and want to then pass the user name back to the server and be able to access it via PHP.

How do I do that?

I have everything in place except how to access the vaule I picked from the DB Grid using Java.

The code below properly displays the selected MySQL record's username. How do I get that information back to the server?
var Rw=DBG_Users.getFocusedRow();
       // fetch the user name (Col2) of the selected record
       var ThisUser=DBG_Users.getTableModel().getValue(2, Rw);

Open in new window

Avatar of jopie916
jopie916

One way would be to put it in a hidden field and submit it in a form:

eg:

<form name="form" action="yourphpfile.php" method="post">
<input type="hidden" name="ThisUser" value="">
</form>

Then:

<script>
document.getElementByName("ThisUser").value = ThisUser
</script>


it is not clear to me what you want , but try one of the

1. Javascript redirection with get parameters
<script>
user=mike
window.location = "http://www.site.com/?action=info&user="+user ....
<script>

2. place a hidden field in your form , like "<input type='hidden' name='user'/>" and update the value via javascipt before do post
Avatar of b0lsc0tt
evault,

Besided the 2 methods recommended above the only other I can think of is to use cookies.  Javascript can't send it direct to PHP script but those 3 things are common between the languages/technologies and can be used to exchange info.  If you want to use a cookie in either and need details then let me know.

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of evault

ASKER

pesmerg: I tried doing that and get 'Error on page'
This is the Java Script exceuted when the button is clicked
 
       document.getElementById('foo').value = 'thenewvalue';
 
this is the HTML code
?>
  <form>
      <input type="hidden" name="something" id="foo"/>
   </form>
 
<?php

Open in new window

Let us see more of the html and Javascript (e.g. form, fields, script, button).  Also click on the message of "error on page" and let us know the error and show us the code for that line.
bol
ASKER CERTIFIED SOLUTION
Avatar of pesmerg
pesmerg
Flag of Türkiye image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of evault

ASKER

Thank you for your assistance. I had already figured out the procedure by the time you had posted the answer.