Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to pass Javascript variables to PHP variables

Posted on 2009-07-09
3
Medium Priority
?
185 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 2000 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …
Suggested Courses

610 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