2

I have a form for asking information about courses , every course has it page, but the information page is one for all. The form should be something like that:

<form action="#" method="POST">
<label for="name">Name</label>
<input name="name" type="text">

<label for="email">Email</label>
<input name="email" type="email">

<input type="hidden" id="code"  value="<?php echo $course_code; ?>">
<input id="submit" type="submit" value="Invia" />
</form>

I wish to change the var $course code according to the referrer page. (With a $_GET var)

I tried "Shortcode Exec PHP" plugin to execute php in wp pages, but doesnt work.

WalterV
  • 1,490
  • 2
  • 21
  • 33
  • This form goes to another page (# is just to simplify the code). I want to compile automatically the hidden field before the form is sent. – WalterV Jan 09 '15 at 15:19

3 Answers3

0

When you POST the form, the variable won't be set in $_GET but in $_POST. It's either one or the other, so if you want to read the $_GET var, you must also use GET on the form, like this:

<form action="#" method="GET">
<label for="name">Name</label>
...

(this is what Fred commented on, but I couldn't expand upon that comment due to my low rep)

Fasermaler
  • 943
  • 6
  • 14
  • However, this seems to be [an inappropriate time to use the GET method.](http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get) – Blazemonger Jan 09 '15 at 15:57
0

I was wrong to use "Shortcode Exec PHP" plugin. I set a shortcode:

$course_name = $_GET['cn'];
$courses= array("courses1","courses2","couses3");
if (in_array($course_name, $courses)) {
   echo $course_name:
}

and the in the wordpress page can be used the name of the shortcode

[couse_name]

Now its work!

WalterV
  • 1,490
  • 2
  • 21
  • 33
0

You can just use $_REQUEST so it doesn't matter if its a POST or a GET from the form. But I wouldn't use GET from a form unless it was a search or something where the user could bookmark the url and see the result. Mostly use POST for all other instances.

HTML form...

<form method="post">
    <label>Name<br>
        <input type="text" name="name">
    </label>
    ...
    <input type="submit" value="Invia">
</form>

PHP page that handles the form...

<?php
    // $_REQUEST will contain POST, GET & COOKIE
    echo $_REQUEST['name']; 
?>
zgr024
  • 1,175
  • 1
  • 12
  • 26