Solved

How to pass Javascript variables to PHP variables

Posted on 2009-07-09
3
180 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
[X]
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
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 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