How to add $_POST['variable'] to URL of next page? - php

I have a simple landing page with a search bar and submit button.
I want the URL of the next page to read: mysite.com/after_search.php?search=*the search text here*.
Here is the code from the landing page / index.php page:
<form class="" action="after_search.php?search="<?php echo $_POST['search_title']?> method="post">
<div class="form-group" id="search_wrapper">
<input type="text" id="search_field" class="form-control color-outline" name="search_title" placeholder="Search">
The issue is that $_POST['search_title'] is not set until the next page loads. So, it does not work because $_POST['search_title'] is not defined on this page.
How do I add the contents of the search bar to the URL of the next page?
EDIT 1
It was suggested that I switch to GET instead of POST.
But on the 2nd page, there are many places that use $_POST['...']
Switching all those to $_GET['...'] has allowed it to work.
Thanks, all

You need to use $_GET method to get the values from a URL:
<form class="" action="after_search.php" method="get">
To get the value, use:
$_GET['search']
If you are passing the value through the link:
?search=the search text here
That means you are using $_GET, thus you need to use $_GET instead of $_POST.

You are getting data, so use GET method and you want adding the variable in url GET method do this.
<form class="" action="after_search.php" method="get">
<div class="form-group" id="search_wrapper">
<input type="text" id="search_field" class="form-control color-outline" name="search" placeholder="Search">

If you have this search_title in URL you can use $_GET['search_title']. If not its unavailable then use a default. Something like
action="after_search.php?search=<?php echo isset($_POST['search_title'])? $_POST['search_title']: ''?>"
Here I used an empty string () as default.

Related

How to combine get method parameter and div id name without javascript?

This is simplified code of index.php :
<form action="index.php" method="get">
<input type="text" name="course">
<button>Find</button>
</form>
Suppose, in the text field "ADAM" is put and after pressing Find button url becomes
myweburl/index.php?course=ADAM
But I want to make it
myweburl/index.php?course=ADAM#courseid
NB: Here courseid is a div id name inside index.php. By this way, I will be able to scroll down the result area.
Do you know how to do this?
Why not do it in a more legitimate way
<form action="index.php" method="get">
<input type="text" name="course">
<input type="hidden" name="courseid" value="<?php echo $courseId;?>">
<button>Find</button>
</form>
Then you get a url like this
myweburl/index.php?course=ADAM&courseid=1234
Now you dont have to do any text manipulation in the script that processes the data you just use
$_GET['course']
$_GET['courseid']
Simply add the hashtag to the action attribute.
The browser would know to move it to the end of the url string (after the GET parameters).
<form action="index.php#courseID" method="get">
<input type="text" name="course">
<button>Find</button>
</form>
Would result:
index.php?course=xxxx#bla
Tested on Chrome, if someone find other results please update.

Pass a post / get Variable using onclick in a div

I am trying to pass a variable (maybe the name of the div), to create an dynamic page without using javascript.
I try to avoid javascript as much as possible, as you could imagine from the first sentence.
My script looks kinda like this:
<?php $topbar = $_GET['topbar']; ?>
<form action="" method="get">
<div id="navigationTopContent" name="start" onclick="window.location=''">
1. entry
</div>
<div id="navigationTopContent" name="group" onclick="window.location=''">
2. entry
</div>
</form>
but it is absolutely not working.
you are already using form to post/get values, just use input type hidden if you want to sent data anonymously.
<input type="hidden" name="abc" value="data">
when using form you must use submit button to post data.
or you can use anchor tag to post data as :
echo 'Entry';
If you are trying to pass a post / get variable, apprently you have to use buttons and correct the css so that the button is looking like your div was. You them the same name and different values. It looks kinds like this:
<form action="" method="get">
<div id="navigationTop" class="text">
<button class="navigationTopContent" name="topnav" value="start">Start</button>
<button class="navigationTopContent" name="topnav" value="group">Groups</button>
</div>
</form>
<?php $topnav = $_GET['topnav']; echo $topnav; ?>
this is not perfect, since the buttons is, due to some magic always a bit darker, but ok.

wordpress plugin redirects instead of performing search

I am creating a plugin; the plugin has item listings, and I want to search for items in the listings.
Here is my code:
<form method="get" action="<?php echo admin_url('admin.php?page=listing'); ?>" id="search" >
<input type="text" name="q" placeholder='Search'/>
</form>
When I submit the form, the URL redirects to:
http://localhost/wordpress/wp-admin/admin.php?q=shock
And no search occurs! How do I fix this?
Your page parameter gets erased.
You are using a GET method and q parameter overrides all other parameters in a query string, which is page=listing.
Two possible solutions:
use POST by setting method="post" attribute, in which case q will be passed as a POST parameter to a page with URL .../admin.php?page=listing.
include the page parameter inside your form (<input type="hidden" name="page" value="listing"/>) and replace admin_url('admin.php?page=listing'); with admin_url('admin.php');, in which case you'll end up on .../admin.php?page=listing&q=shock, because your form will contain both q and page parameters.
The choice really depends on how your plugin obtains the search string: GET or POST parameter.

Adding parameters from a FORM to URL with queried page

I have a search page which is queried from index.php
example : http://localhost/sgc/admin/index.php?page=search
then i created a form within this page to search users by names
<form role="form" method="get" action="" class="form-horizontal">
<input type="search" name="name" class="form-control" placeholder="username">
</form>
the problem is when the input is submitted it redirects to the index.php and adding the paramteres to the index page http://localhost/sgc/admin/index.php?name=
required URL format is to be http://localhost/sgc/admin/index.php?page=search&name=
can it be achived using php or i have to use javascript
Thanks, and sorry for my bad english :)
try :
After submision :
your url : ttp://localhost/sgc/admin/index.php?page=search&name=YOUR_STRING

Can php a href post not use a link like index.php?id=value

I want to post some value through a href. However, I do not like to use a link like index.php?id=value. Is there any other method?
I want to post test1 and test2.
<div id="div1">
<li>test1</li>
<li>test2</li>
</div>
<div id="div2">this is a <? $_POST["name"]; ?>, just a test.</div>
Use a form, with method=POST, and a hidden form field for the value you want to post. The 'href' either becomes a submit button, or have an onclick action on the link that just submits the form.
You can apparently use PHP for this, and the information is supposed to be here, but it is currently not working:
http://www.zend.com/zend/spotlight/mimocsumissions.php
Otherwise, you should probably just do this:
<form id="myForm" method="post" action="index.php">
<input type="hidden" name="id" value="value">
<input type="hidden" name="somethingElse" value="test">
<!-- more stuff you want to post -->
</form>
Post!
The form is invisible, but you need one form per link like this (or I guess you could use only 1 form total and call another javascript function from the onclick event that modifies the values of the form fields). I don't really see the benefit though.
You could use either jQuery/ajax to post your data to a backend or post the variables as said in the answer above.
You can use mod_rewrite (if you are using Apache) to modify the look of url. e.g. instead of www.test.com/index.php?id=value you can use www.test.com/value or www.test.com/index/value

Categories