1

This may not be possible, which is why I can't find the answer, but is it possible to pass variables from HTML to PHP without a form?

I'm trying to create a template that will use HTML I import into a Wordpress Page. I want to be able to put certain divs in different parts of the template. Think of a table, where I put the divs in different cells.

Any help you can give me would be greatly appreciated. Thank you!

edit: I haven't created the template code yet, since I'm not sure how to without the answer to my question. But here's an example of the code I'm pulling from:

<div id="text-column">

<div class="scarlet_underline"> <p class="toc_dept"><a href="XXX"><img src="XXX" width="112" height="36" alt="Forum" border="0"/></a></p> <p class="toc_item"><a href="XXX">University futures; Science’s influence; Can geoengineering be green?; Mineral reserves; Transforming conservation; Intelligent transportation; Personal health records.</a></p> </div>

<div class="scarlet_underline"> <p class="toc_dept"><a href="XXX"><img src="XXX" width="112" height="36" alt="From the Hill" border="0"/></a></p> <p class="toc_item"><a href="XXX">House approves bill to reform offshore oil drilling; Senate committee approves competitiveness bill; GAO investigates genetic test companies; House, Senate committees lay out plans for NASA’s future; Federal science and technology in brief.</a></p> </div>

So ideally, these two "scarlet_underline" divs would each be pulled separately, maybe by using the alt attribute? The template code would be something like:

<div class="forum"><?php code that pulls that div ?></div>

<other code other code>

<div class="hill"><?php code that pulls that div ?></div>

2
  • 4
    Can you show a bit of what your code is? Commented Jan 4, 2013 at 20:37
  • This is ambiguous. Please explain the process. Commented Jan 4, 2013 at 20:42

1 Answer 1

2

Is it possible to pass variables from HTML to PHP without a form?

Yes, that is possible. PHP automatically creates variables for Cookies and parameters in the Query part of the request URI. Both can be send from the browser without any HTML form.

http://example.com/path/to/index.php?type=animal&name=narwhal#nose
\__/   \_________/ \_______________/ \_____________________/ \___/
 |          |             |                    |               |
scheme   authority       path                query          fragment
                                             ^^^^^

This request URI will make PHP create two entries in the $_GET superglobal:

$_GET['type'];    # animal
$_GET['name'];    # narwhal

See as well Variables From External SourcesDocs.

Sign up to request clarification or add additional context in comments.

5 Comments

Nice diagram but how does it help?
It explains the construct of a URL and how variables can be passed to PHP in the query string.
Thank you hakre, that does help my understanding of PHP, but I think the code I want to use is too long for a url. (Not your fault, I didn't specify. I've added some sample code in my original post.)
Yes that can be. If it is too long for the URL, then you need to do a POST request which is normally a form. Technically you can do that with javascript (kown as AJAX) without having a form. If it's too large, you need to learn about AJAX first (it's not that complicated and Wordpress ships with Jquery which helps your for the javascript part of AJAX. See also codex: codex.wordpress.org/AJAX
Thank you hakre! I am looking into AJAX now, and hope I'll be able to make it work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.