How use $_GET variable in a Wordpress page - php

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.

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)

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!

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'];
?>

Related

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

Fetch value of a input type and store in variable

I have following input type
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
Its value is value="EXTERIOR". I want to fetch the value and store in a variable. I am not getting any idea regarding this. I am trying to modify plugin (career portfolio) according to my project and its part of that.
Your html form:
<form method="POST" action="process.php">
<input type="text" name="foo">
<input type="submit">
</form>
Your Php form processing page (process.php):
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$foo = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING);
}
?>
This is front end code, if you are trying to move front end data to server side it must pass through the server. i.e. $_POST...
now you can either do this with AJAX / PHP or PHP / HTML only
<?php
if(isset($_POST[project.title])
$var = $_POST[project.title]; //its now in a varible
?>
<form method="POST" action="thispage.php">
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
<input type=submit value="PRESS ME TO SUBMIT VALUE TO VAR">
</form>

Using PHP to pass a form field as URL parameter

I have a form field:
<input type="text" value="" name="Email" id="Email">
and my form action confirmation url is this:
http://.../index.php?Email=<?php echo $_POST['Email']; ?>
However after submitting, the Email parameter is not coming through. Is this something that can be done in PHP, or does it only read the field on initial page load?
Thanks
Your issue is that you are mixing $_GET and $_POST.
See your code here, http://.../index.php?Email=<?php echo $_POST['Email']; ?>, when you post to that one, there will no longer be a $_POST['Email'], but a $_GET['Email']. So the first post will likely work (if you are using <form method="post" action="...">), but the second submit will fail, as $_POST['Email'] no longer exists.
So I recommend that you don't use parameters in the action. Instead, put them in a hidden field or switch to only $_GET parameters.
Option 1, use hidden field
Change the form on your second page to:
<form action="http://.../index.php" method="POST">
<input type="hidden" name="Email" id="Email" value="<?php echo $_POST['Email'];?>" />
...
</form>
Option 2, use only $_GET
Change the form on your first page to <form ... method="GET">
And then change the form on the second page to use $_GET['Email'] and the method to GET.
<form action="http://.../index.php??Email=<?php echo $_GET['Email'];?>" method="GET">
...
</form>
Option 3, Use $_REQUEST instead of $_POST
Simply use http://.../index.php?Email=<?php echo $_REQUEST['Email']; ?> as your action url, as $_REQUEST is a merge of $_GET and $_POST. Be aware that this is a merge of $_GET, $_POST and $_COOKIE.
It depends on the your FORM method
Your form should be
<form method='post' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
<?php
$emailValue = $_POST["Email"];
//Use variable for further processing
?>
if your form is as below (please check that method is get
<form method='get' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
<?php
$emailValue = $_GET["Email"];
//Use variable for further processing
?>
You don't need to define the structure of the GET request; that's what the form does.
For instance:
<form action="workerbee.php" method="GET">
<input type="text" name="honey_type" value="sweet" />
</form>
...when submitted, would automatically append the field - honey_type - to the URL for you. It would end up like this:
http://example.com/workerbee.php?honey_type=sweet
You can then access the value via $_GET['honey_type'] in workerbee.php. To pre-fill the form with the existing submitted value - assuming that workerbee.php holds the form - just add a conditional value parameter:
<?php
$honey_type = !empty($_GET['honey_type']) ? $_GET['honey_type'] : null;
?>
<input type="text" name="honey_type" value="'<?php echo htmlspecialchars($honey_type); ?>'" />
If you're trying to use data from your current form, your form tag should read as follows:
<form action="http://.../index.php" method="GET">
If you're trying to pass data your server already has (such as from a previous form), then you should use a hidden field instead:
<input name="email" type="hidden" value="<?php echo $_POST['Email']; ?>">

Sending form POST request to PHP

I want to send a couple of form fields as a POST request to my PHP page, but I can't get it to work. Here is my code:
PHP login.php
<?php
if(!ISSET($_POST["username"]) && !ISSET($_POST["password"])) {
include "login.html";
}
else {
echo "hi";
}
?>
HTML login.html
<form action="login.php" method="post">
<label for="username">Username</label><input type="text" id="username"/>
<label for="password">Password</label>Password<input type="password" id="password"/>
<input type="submit" value="Submit"/>
</form>
Can anyone spot my mistake?
Your inputs do not have names. The id is used for client-side referencing, but it is the (non-unique) name attribute that is used to determine the key for a value when the data is submitted. A form control cannot be successful (i.e. in the form data) without a name.
You haven't included the name attribute in your html input elements. name attribute is used when passing form information to the webserver. id is primarily used for javascript based manipulation.
Username<input type="text" name="username"/>
Password<input type="password" name="password"/>

Can i Mix $_get and $_post?

in order to edit my entries i want to:
<form id="pregunta" name="pregunta" class="form_pregunta" method="post" action="pregunta.php?id=26">
<h2>Titulo de la pregunta</h2><input name="q" id="q" class="q" value="este es mi títiulo " type="text">
<h2>Describe tu pregunta</h2>
<textarea name="texto" id="texto" style="width: 98%;"><p>esta es mi descripcion</p></textarea>
<h2>Etiquetas</h2>
<input name="tags" id="tags" onmouseover="mostrar_tooltip('nube_e','','0','70','')" onmouseout="ocultar_tooltip('nube_e')" value="dos,tres,una,">
<input name="responde_a" style="display: none;" id="responde_a" value="0">
<button name="pregunta" id="pregunta" type="submit">form_edit_question_button</button>
</form>
And then in file.php
i'd like to $_get['id'] and $_post['inputs']
but when i go:
if(isset($_POST['edit_pregunta'])){
echo 'lalalalalalalalalalalalalalala';
post_edit_pregunta();
}
it won't ever enter :S. is that normal or i'm missing something... i wanted not to have a hidden input with the id of the post i want to edit..
I'm not 100% sure, but forms don't send their name when submitted, much less their id.
You could do the following instead:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
<input type="hidden" name="edit_pregunta" value="anything">
... //inputs here
</form>
and your if should now enter.
It looks like you're checking for your form's "id" attribute. This is not sent when the form is submitted, only values in <input>, <select>, <textarea> and <button> are sent.
You should check for one of those.
Edit: Your button name is "pregunta", so that is the POST variable you should be checking for, eg
if(isset($_POST['pregunta'])){
Just to comment in general on mixing params in the form's "action" and inputs, you can mix them as long as the form method is "post". You cannot set GET params in the form's action and use the "get" method
<!-- Good -->
<form action="proc.php?id=123" method="post">
<input name="foo" value="foo">
<input type="submit">
</form>
<!-- Bad -->
<form action="proc.php?id=123" method="get">
<input name="foo" value="foo">
<input type="submit">
</form>
There should be no problem at all with having get and post variables in the same request, but are you sure your syntax is correct? If this is normal php, shouldn't you write
<form id="edit_pregunta" method="post" action="file.php?id=<?php echo $this->id; ?>">
... //inputs here
</form>
[Edit]
The problem is (if I'm correct and this is standard php) that you generate a form that looks something like this:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
... //inputs here
</form>
This will make id look like this: '$this->id' (including the '-signs). When what you want is something like this:
<form id="edit_pregunta" method="post" action="file.php?id=51">
... //inputs here
</form>
Then $_GET['id'] would be 51.
[Edit2]
Also, I think you need to change
if(isset($_POST['edit_pregunta'])){
with
if(isset($_POST['pregunta'])){
If I'm not mistaken the name of a form doesn't get sent to the server, however, the name of the submit-button does, but I might be wrong about that part.
Yes you can, I've done it several times.
Probably something else is wrong with your code.
Is there any control with name="edit_pregunta" or is it just the id of the form? IDs are not sent to the server.
Simply adding the id to the form will not create the $_POST['edit_pregunta'] you verify.
Instead, inside the form tag, add an <input name="foo" />; in the php script verify $_POST['foo']
While the HTTP spec doesn't disallow query parameters in POST methods, it is somewhat unusual. You'd be better off using a hidden input field in the form to pass any non-user values up to the script.
That said, the syntax for your form is wrong. You need to use "echo" to insert the value of $this->id into the action.
Use input type="submit" in place of button tag.
You need name for form submission and activate php script!
HTML Code:
<form action="change.php" method="POST">
<input type="password" name="p1" class="change_text" placeholder="New Password"/></br>
<input type="password" name="p2" class="change_text" placeholder="Re-Password"/></br>
<input type="submit" name="change" value="Change Password" id="change" />
</form>
PHP Code:
<?php
if (isset($_POST['change']) {
$p1=$_POST['p1'];
}
?>

Categories