I have a form that is going to submit a search query with PHP via a MySQL database. The form action attribute is set to another page, namely search.php. I want to store the search term in a variable, $searchQuery, but when I try and echo that variable out in the HTML, it doesn't show any value and I'm not getting any errors?
This is the first time I've built a search form in PHP, but I don't understand why this variable isn't being echoed?
The echo "test string"; line is being outputted as expected.
OUTPUT - search.php page
<div>
<?php
if(isset($_POST['search-submit'])) {
$searchQuery = $_POST['search-submit'];
if (isset($searchQuery)) {
echo $searchQuery;
echo "test string";
}
}
?>
</div>
FORM in the header of every page
<form action="search.php" method="POST">
<label for="search">Enter Search</label>
<input type="text" name="search" id="search">
<button type="submit" name="search-submit" id="search-submit">Search</button>
</form>
if you are demanding value from your button it should be
<button type="submit" name="search-submit" value="add value here" id="search-submit">Search</button>
but if you are demanding value from your input text it should be
$_POST['search'] "search" which is your input text name not $_POST['search-submit'] "search-submit"which is your button name
Related
I'm currently busy with (re)making the dutch Wikipedia site. And I want to make the search bar. My idea was to get text input and when you click on search the text will go to the front of a link like this:
https://ibb.co/XbndsKP
This is currently the search bar:
<div align='center'>
<form method="submit" action='php/zoeken.php'>
<input placeholder="zoeken"id="zoeken"class='zoeken'type="text">
<button class='button'type="submit">zoeken</button>
</div>
<form action="THE PAGE YOU SENT REQUEST" method="POST">
<input placeholder="zoeken" name="zoaken" type="text">
<input type="submit" value="Submit">
</form>
<?php
/* Get the text input with $_POST */
$string = $_POST['zoaken'];
/* Redirect the page to the new address */
header('Location: https://nl.wikipedia.org/wiki/'.$string);
You should also check if the text is null, empty or has vulnerability.
The better way it's use JS. Get value of input and redirect like this
document.location = 'http://nl.wikipedia.com/search_word';
i am new to php and while i am practing i came across a problem. actually,i have two files index1.php and index2.php. in index1.php i have a link with a unique id as
<a href="index2.php?companyid=<?php echo $row('company_id');?>>details</a>
i have got this value in index2.php as
if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
now i have a search form in the index2.php as
<form method="POST" action="index2.php">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
now on button click i want the search results be displayed in the same page as
'index2.php?companyid=$companyid'
but some how if i try to use $_POST['submit']; in the same page it takes me to index2.php and instead of index2.php?companyid=$companyid and also it throws error as undefined index of $companyid if i don't use $_POST['submit']; and echo $companyid; it gives value and works fine. all i want is that to use $companyid' value inside ``$_POST['submit']; as and display the result in the same url as before
if(isset($_POST['submit']){
$companyid //throws an error index of company id
}
any help will be appreciated
First off, it looks like you are not using the company id in the form itself, so it will not be submitted as part of the the POST. You could possibly use:
<form method="POST" action="index2.php">
<?php if (isset($companyid)): ?>
<input type="hidden" name="companyid" value="<?= $companyid; ?>">
<?php endif; ?>
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
But you would probably also need to change your logic to:
if(isset($_POST['companyid'])){
$companyid = $_POST['companyid'];
}else if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
As Josh pointed out in the comments, PHP is not able to remember your previous GET request but this can easily be solved by altering the action attribute of the form element. By doing this you can pass on the previous data. This would look a little something like this:
<form method="POST" action="index2.php?companyid=<?php echo $companyid;?>">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
This way you will be redirected to index2.php with the URL parameters present and you will be able to retrieve both search and companyid using $_POST and $_GET or use $_REQUEST for both.
I am new to both HTML and PHP and I encountered a problem when working on some simple projects. Lets say I have a text bar on my webpage and I want to display the text written in text bar on webpage after the user enters some text and presses the submit button. My problem is that the webpage shows the output when the webpage first loads. Is there a way to prevent the php code from executing untill the submit is pressed ?
Here is a sample code code that indicates the problem I am referring to.
<html>
<body>
<form action="./index.php" method="GET">
First Name: <input type="text" name="first" maxlength="50"><br/>
<input type="submit" value="GO"/>
</form>
</body>
</html>
<?php
$text_var = $_GET[first];
echo "This was typed into text bar" . $text_var;
?>
So "This was typed into text bar" is outputted right away when website loads.
I want it to be outputted only after submit button is pressed.
Thanks.
you need to split it up the form should be showed if nothing is submited so either check the value or the submit button
make sure you keep the html format. look at label tags to describe form inputs
<html>
<body>
<form action="./index.php" method="GET">
<label for="first">First:</label>
<input id="first" type="text" name="first" maxlength="50"><br/>
<input type="submit" value="GO"/>
</form>
<?php
if (!empty($_GET['first'])) {
//take care you escape things never output user input (XSS)
$op = htmlspecialchars($_GET['first']);
echo "This was typed into text bar" . $op;
}
?>
</body>
</html>
Check if $_GET['first'] exists. This is is usually done as the following:
View
<form action="index.php" method="post">
<!-- input fields here -->
<input type="submit" name="submit" value="GO"/>
</form>
Controller
<?php
if (isset($_POST['submit'])) {
// process post
} else {
// display the form
}
<?php
if(isset($_GET['submit'])){
$text_var = $_GET[first];
echo "This was typed into text bar" . $text_var;
}
?>
i am retrieving data form database using a search query.
PHP code (which I'm using in search query to display search results)
echo "<span style='background-color= #FFFF00'>$query</span><br>";
$count=$dbo->prepare($query);
$count->execute();
$no=$count->rowCount();
if($no > 0 ){echo " <span>No of records = ".$no."</span>";
echo "<table><tr><th>PHONE NUMBER</th><th>OWNER NAME</th></tr>";
foreach ($dbo->query($query) as $row){
echo "<tr><td>$row[ROLLNO]</td><td>$row[CNAME]</td></tr>";
}
echo "</table>";
i want to do like this,
when a user clicks on a phone number, it should redirect to a new page and in that new page, my input box should be filled with this phone number and should be submitted.
Input Box Code (which I'm using in page 2)
<form name="phone_number_form" id="phone_number_form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="return vali()" >
<input type="text" name="number" id="number" />
<input type="submit" name="submit" value="Submit" />
</form>
Looking at w3schools PHP has a $_POST variable which is used to collect values from a form sent with method="post". There's also $_GET and $_REQUEST which seems to merge both post and get data. http://www.w3schools.com/php/php_post.asp
There are a couple of options like making a request (get) from your first page or post the data from your first page.
REQUEST Method
Heres how I think the request way to do it would work
PAGE 1
Amend the foreach that renders the table row to include an hyperlink to your second page
foreach ($dbo->query($query) as $row){
echo "<tr><td>$row[ROLLNO]</td><td>$row[CNAME]</td></tr>";
}
PAGE 2
Amend the textbox to be populated with the phone number from the request variable
<input type="text" name="number" id="number" value="<?php echo $_REQUEST["phoneno"]; ?>" />
POST Method
In your first page using javascript when the user clicks the phone number set a hidden field and submit the form to the second page. Again you should be able to read the hidden fields value from the $_REQUEST variable
Is to possible to POST a string in the form field and get converted string result in the same form field?
I use the code:
<?php
$string='';
if (isset($_POST['string']))
$string=$_POST['string']
if (isset($_POST['DoStuff']))
{
$string = doStuffWithThisString($string);
}
if (isset($_POST['DoOtherStuff']))
{
$string = doOtherStuffWithThisString($string);
}
?>
<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
<input type="text" value="<?=$string?>" name="string" />
<!-- <?=$string?> is the same as <?php echo $string; ?> -->
<input type="submit" value="Do Stuff" name="DoStuff" />
<input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>
but get the result above form field...
Are you sure short tags are enabled?
See: http://php.net/manual/en/ini.core.php
If they are not, just use:
<?php echo $string; ?>
I don't see why it wouldn't work.
Depending on the browser, the button names may not be "DoStuff" and "DoOtherStuff". For example, in IE it will be $_POST['DoStuff_x'] and $_POST['DoStuff_y'].
do a print_r($_POST); to see what the form data is being posted as.
If you would use the same name in the submit fields, upon page reload in $_POST['name'] you would get the value you clicked on.
I think that's the solution to the issue, but can someone confirm this ?