Solved

How to pass Javascript variables to PHP variables

Posted on 2009-07-09
3
177 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

831 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