Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to pass Javascript variables to PHP variables

Posted on 2009-07-09
3
178 Views
Last Modified: 2013-12-13
Hi:

We'd like to perform the following code activities:

- Get values from text boxes of FORMS using javascript and XHTML code (we can do it successfully and we print out them to verify)
- We want to assign these values into PHP avariables like this

<?php
       $author = document.getElementById( 'author' ).value;
         // call php function
?>

We tried and error occurs --> invalid call document.getElementById( 'author' ).value;
How can we do it?

If you've some idea, please help.

Somits
0
Comment
Question by:somits
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
martinewski earned 500 total points
ID: 24813812
You are actually using a javascript command into php code. That won't work.

It depends on the method you are using on your form. See below:

// IF YOUR FORM USES A POST METHOD
$author = $_POST['author'];
 
//IF YOUR FORM USES A GET METHOD
$author = $_GET['author'];

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24813950
There are two ways to do this, one is fairly simple.

If you have a form that will be submitted then you can create a dummy DIV in the form and inject  <INPUT ../> tags in to the DIV so that when the form is submitted then your javascript variables go with the form

The second method is to communicate directly with the server using AJAX via the XMLHTTP object. This is more complicated but it requires no pressing of submit buttons.

Otherwise there is no DIRECT way to subsitiute javascript values into PHP variables as Javascript is a clientside script and PHP is a server-side script. They run only completely different computers.

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24814008
Here is a link to using an AJAX solution. It looks winded because I went out of my may to explain how it works. It could be adapted to your problem.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23107627.html
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question