search form action url not display php variable - php

hi can anyone tell me whats problem in this form . its not show varible in url
<form class="navbar-form navbar-left" method="post" action="test.php?q=<?php echo $searchb;?>" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
php code here
if (isset($_POST['searchb'])) {
$searchb = $_POST['searchb'];
}
when something input in form and action url not show any value
test.php?q=
but we echo variable its show value .

First time that form loaded $_POST['searchb'] is empty so action is equal test.php?q= after load form when you submit form then $_POST['searchb'] to be filled

The Part: action="test.php?q=<?php echo $searchb;?>" is first illogical and most importantly unnecessary since you are POSTing your form. It would have been valid if $searchb was pre-defined. However, since it is a part of the Form; it will always be NULL since it was never declared but expected to be dynamically added on Form-Submit, which wouldn't happen. You do it in one of the 2 ways:
OPTION #1 - PASSING q VIA HIDDEN INPUT:
<!-- YOU DON'T NEED THE echo $searchb PART IN YOUR FORM'S ACTION BECAUSE -->
<!-- THAT VALUE IS NOT PART OF THE ACTION AS IT IS NOT EVEN SET AT ALL -->
<form class="navbar-form navbar-left" method="post" action="test.php" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
<!-- ADD THE q AS HIDDEN INPUT ELEMENT WITH A VALUE -->
<input type="HIDDEN" name="q" value="Some value" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
if (isset($_POST['searchb'])) {
$searchb = $_POST['searchb'];
}
OPTION #2: USING GET & SETTING Q TO A PRE-DEFINED VALUE
<?php $param = "some-predefined-value"; ?>
<form class="navbar-form navbar-left" method="GET" action="test.php?<?php echo $param;?>" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
// BUT REMEMBER TO CHECK INSIDE THE `GET` GLOBAL
if (isset($_GET['searchb'])) {
$searchb = $_GET['searchb'];
}
BETTER OPTION FOR YOUR USE-CASE: USING GET & SETTING Q FROM THE INPUT
<!-- STILL NO NEED FOR SETTING QUERY PARAMETERS MANUALLY-->
<!-- THE GET METHOD WOULD TAKE CARE OF THAT FOR YOU ONCE THE FORM IS SUBMITTED -->
<form class="navbar-form navbar-left" method="GET" action="test.php" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<!-- NOTICE THAT THE NAME OF THE INPUT FIELD CHANGED TO; q HERE -->
<input type="text" name="q" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
// BUT REMEMBER TO CHECK INSIDE THE `GET` GLOBAL
if (isset($_GET['q'])) {
$searchb = $_GET['q'];
}

Related

php can't get the text from a text area

I can't figure out why this doesnt get the value of the textarea. I've been to loads of stackoverflow posts and I can't figure out what's wrong. I've tried getting values from textfields, from dropdown lists etc and they all work I just can't get the textarea to work. I've using _GET instead, still didnt work.
This is the message I get if I don't use the isset function: Notice: Undefined index: descri.
Here's the HTML:
<form role="form" action="saveform.php" method="post" name="eventform">
<div class="form-group">
<label for="descri">Description</label>
<textarea name="descri" form="eventform" style="resize:none"></textarea>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>
PHP:
<?php
if(isset($_POST['descri']))
{
echo htmlspecialchars($_POST['descri']);
} else {
echo "DOESNTWORK";
}
?>
Just remove form attribute from textarea:
<textarea name="descri" form="eventform" style="resize:none"></textarea>
Remove form attribute from textarea that is located inside the form:
<form role="form" action="saveform.php" method="post" name="eventform">
<div class="form-group">
<label for="descri">Description</label>
<textarea name="descri" style="resize:none"></textarea>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>
You have to use it only when your textarea is outside the form (remember form should have id not just name:
<form role="form" action="saveform.php" method="post" id="eventform">
<div class="form-group">
<label for="descri">Description</label>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>
<textarea name="descri" form="eventform" style="resize:none"></textarea>
Remove form="eventform" attribute from your <textarea> element. You do not need to set it as your <form> will post your data.
<form role="form" action="saveform.php" method="post" name="eventform">
<div class="form-group">
<label for="descri">Description</label>
<textarea name="descri" style="resize:none"></textarea>
</div>
<button type="submit" class="btn btn-default" id="addform">add</button>
</form>

Pass parameter through php vaiable from form to another php file

I have next html form:
<form class="form-horizontal" action="frames1.php" method="post">
<div class="form-group">
<label for="uri1" class="control-label">URL:</label>
<div class="controls">
<input type="text" id="uri1" placeholder="www.ejemplo.com">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
<input type="submit" class="btn btn-primary" id="submit" value="Visualizar" />
</div>
</form>
I want to pass the value from textbox "uri1" to a php variable.
Thats my frames1.php:
<?php
$uriValue = $_GET["uri1"];
?>
<script>
var uri = '<?php echo $uriValue;?>';
alert(uri);
</script>
But the system shows me "", not the value I put on the textbox in the form just before.
How can pass the value from the textbox to read on the php file?
Thanks!
You are using method as post in form co change this to
<?php
$uriValue = $_POST["uri1"];
?>
<script>
var uri = '<?php echo $uriValue;?>';
alert(uri);
</script>
and also add name attribute to your field. name="uri1"
that is,
<form class="form-horizontal" action="frames1.php" method="post">
<div class="form-group">
<label for="uri1" class="control-label">URL:</label>
<div class="controls">
<input type="text" id="uri1" name="uri1" placeholder="www.ejemplo.com">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
<input type="submit" class="btn btn-primary" id="submit" value="Visualizar" />
</div>
</form>
Here is the working link.
You send a form by POST but in your frames1.php file, you try to get the content with $_GET. So what you have to do is just change GETto POST, so it looks like
<?php
$uriValue = $_POST["uri1"];
?>
Also a bigger problem is, that your input doesn't have a name, the parameter can only be referenced by the name, not by the ID. So your input should look like that:
<input type="text" id="uri1" name="uri1" placeholder="www.ejemplo.com">
There are one mistake in HTML as well as PHP code, You have to give name to the input field's. When form is submitted to the server then It will recognize fetch values using HTML Input Field names, and If your posted form in HTML, then you have to use same method to get the values.
Try to replace bellow code with your code:
<form class="form-horizontal" action="frames1.php" method="post">
<div class="form-group">
<label for="uri1" class="control-label">URL:</label>
<div class="controls">
<input type="text" name="uri1" id="uri1" placeholder="www.ejemplo.com">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
<input type="submit" class="btn btn-primary" id="submit" value="Visualizar" />
</div>
</form>
Coming to your frames1.php:
<?php
$uriValue = $_POST["uri1"];
?>
<script>
var uri = '<?php echo $uriValue;?>';
alert(uri);
</script>
Let me know still not resolved.
Diff in HTML file is:
<input type="text" name="uri1" id="uri1" placeholder="www.ejemplo.com">
you didn't mention the name, then server will not recognize the element.
In PHP:
Your using POST method but your getting the values using _GET[], both are incompatible methods. So you will not get the values which are submitted by the form
<?php
$uriValue = $_POST["uri1"];
?>
You have set form method="post".
Therefore, after your form gets submitted, the value should be:
$uriValue = $_POST["uri1"];
Not
$uriValue = $_GET["uri1"];
Solutions:
1) Either change your form method method="post" to get your existing code working.
2) Change $uriValue = $_GET["uri1"]; to $uriValue = $_POST["uri1"];

Can't access form variable with php

I'm new to Web development and have been messing around with BootStrap. I've been trying to make a Web form on my new website, but I can't seem to access the username variable.
Here's my form code:
<form class="navbar-form navbar-left" role="search" action="username">
<div class="form-group">
<input name="username" type="text" class="form-control" placeholder="Username">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
And here's my index.php in the username folder:
<?php
echo $_POST['username'];
echo 'fail';
echo 'username';
if(isset($_POST['username'])) {
$name = $_POST['username'];
echo 'success';
}
?>
With my debugging, I get the message fail to show up, but not the username, so I assume I am doing something wrong either with setting the username, or accessing it.
According to your description, your HTML code should look something like this:
<form method="post" class="navbar-form navbar-left" role="search" action="username/index.php">
<div class="form-group">
<input name="username" type="text" class="form-control" placeholder="Username">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
You could also use <input class="btn btn-default" type="submit" value="Search"/> instead of <button type="submit"></button>.
You have the following mistakes
You didn't set the method attribute in your form.So when the form is submitted the data will be posted via get method.And you need to access $_GET variable for getting the form data. So here when you submit the form the data will be in $_GET variable.But you are trying to access $_POST variable in your code.So you can try like setting the method attribute to method="post in your form and continue with the same code in index.php or try changing $_POST to $_GET` in your index.php and making no changes in your form
You are definging the code in index.php when the form is submitted.So you need to set the action="index.php" in your form
So it will be like this finally
<form class="navbar-form navbar-left" role="search" action="username/index.php" method="post">
<div class="form-group">
<input name="username" type="text" class="form-control" placeholder="Username">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
index.php
<?php
echo $_POST['username'];
echo 'fail';
echo 'username';
if(isset($_POST['username'])) {
$name = $_POST['username'];
echo 'success';
}
?>
OR
<form class="navbar-form navbar-left" role="search" action="username/index.php">
<div class="form-group">
<input name="username" type="text" class="form-control" placeholder="Username">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
index.php
<?php
echo $_GET['username'];
echo 'fail';
echo 'username';
if(isset($_GET['username'])) {
$name = $_GET['username'];
echo 'success';
}
?>
Set your action attribute to index.php and set your method="post" if you access the username with $_POST['username'].

How to return php code into html body

I am building a simple form script that collects a users email and returns a PIN.
The input sits in standard HTML below:
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
I have the following if statement that checks the database to see if the email already exists, and if the user would like a PIN reminder.
if($num_rows != 0) {//if email found in table
?>
Email already registered, would you like a PIN reminder?
<form method="POST" action="">
<input type="submit" name="srSend" value="Click here to get pin reminder" />
<input type="hidden" name="srEmail" value="<?php echo strtolower($email);?>" />
</form>
<?php
exit;
}
At the moment, this returns the result to the user as a new page; how do I put this in the actual HTML of the body page, so it would actually appear below the original form input in a new <p> element?
This is a piece of cake with jquery.post
include the jquery library in your html head and you'll need a short script to get the php content by ajax
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
$('#yourForm').submit(function(e){
e.preventDefault();
var data=$('#yourForm').serialize();
$.post('yourphp.php',data,function(html){
$(body).append(html);
});
});
});
</script>
</head>
<body>
<p>
<form id="yourForm" class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
</body>
You could able to achieve that without ajax too. Simply use a hidden iframe in your main page, then set the target attribute of your form to the iframe as follows:
<form method="post" action="page.php" target="myIframe">
.....
</form>
<p id="theResultP"></p>
<iframe src="#" name="myIframe" style="visibility: hidden"></iframe>
The question now, How could you make the page loaded in the iframe "page.php" to interact with the opener page to update the p. This may be done as follow:
//page.php
<script>
result = parent.document.getElementById('theResultP');
result.innerHtml = "<b>The message you want</b>"
</script>
I dont know if I get what you asking for,but this is my solution :
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
<?php
if (isset($message)){
<p><?php print $message ?></p>
?>
<?php
}
?>
and in top of your file write something like this :
<?php
if($_post['srSend']){
$message='write your message here';
}
?>

Zend get value from textbox and add it into search url

I have question,this is the following code.
<form action="" method="post" id="search">
<div class="row collapse">
<div class="ten columns mobile-three">
<input type="text" size="25" name="search" value="<?php echo $this->search ?>"/>
</div>
<div class="two columns mobile-three">
<input type="submit" name="/search/index/keyword/" value="Search"; ?>" class="button expand postfix" id="search_button"/>
</div>
</div>
</form>
when I click the submit button,the url should be
from id/ to id/search/index/keyword/(here's the search key)
HTML
<form action="" method="post" id="search" name="search" onsubmit="submitForm();">
<div class="row collapse">
<div class="ten columns mobile-three">
<input type="text" size="25" name="search" id="search_value" value="<?php echo $this->search ?>"/>
</div>
<div class="two columns mobile-three">
<input type="hidden" value="/search/index/keyword/" id="search_url"/>
<input type="submit" name="search" class="button expand postfix" id="search_button"/>
</div>
</div>
</form>
JS
<script>
function submitForm()
{
var search_button = document.getElementById('search_url').value;
var search_text = document.getElementById('search_value').value;
document.searchForm.action = search_button + '/' + search_text;
document.searchForm.submit();
return false;
}
</script>
PHP
<?php
if (isset($_POST['search'])) {
// add action code here
}
?>
I'd use a server-side language for that. Make a process page (unless you're familiar with AJAX), get the values by $_POST and make the process page redirect.
I'm pretty sure that it's possible using JavaScript as well, but I'm really bad at it so don't count on my solution.
You can use JS to accomplish your goal.
1) make a function that runs when user clicks submit button.
2) function grabs the word from search box.
3) function changes the "action" attribute on the form element
4) form is submitted

Categories