Solved

How to pass Javascript variables to PHP variables

Posted on 2009-07-09
3
175 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now