Can't access form variable with php - 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'].

Related

Undefined Variable In PHP Passing input name in url

I have html form with post method. I want to pass the value of an input field in the url to the action page. I get undefined variable at first, but when I re-submit the form, I don't get undefined variable, I get the value I want.
Here is the code.
<div class="col-md-6 p-2">
<?php
if(isset($_POST['search'])){
$keyword = $_POST['keyword'];
}
?>
<form method="post" action="search.php?keyword=<?php echo $keyword;?>">
<div class="input-group">
<input type="text" name="keyword" class="form-control" placeholder="Enter a keyword eg: chinese">
<div class="input-group-append">
<button type="submit" name="search" class="form-control btn btn-success"><i class="icofont-search"></i> Search Videos</button>
</div>
</div>
</form>
<p></p>
</div>
You should declare $keyword variable before the if statement.
Try this
<?php
$keyword = null;
if(isset($_POST['search'])){
$keyword = $_POST['keyword'];
}
?>
Or
<form method="post" action="search.php?keyword=<?php echo $keyword ?? "";?>">
Problem in your example is this line <form method="post" action="search.php?keyword=<?php echo $keyword;?>"> this will return empty value in url.
you need a page to submit form in form action, so your codes should look like this:
<div class="col-md-6 p-2">
<?php
if(isset($_POST['keyword']) && !empty($_POST['keyword'])){
echo "Please type something to search";
}
?>
<form method="post" action="search.php">
<div class="input-group">
<input type="text" name="keyword" class="form-control" placeholder="Enter a keyword eg: chinese">
<div class="input-group-append">
<button type="submit" name="search" class="form-control btn btn-success"><i class="icofont-search"></i> Search Videos</button>
</div>
</div>
</form>
<p></p>
</div>
form action will pass parameters in url
And in search result page search.php :
if(isset($_GET['keyword'])){
$keyword = $_GET['keyword']; //Use filter value before searching database.
echo $keyword;
}

search form action url not display php variable

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

How to Pass Input Value in Form to URL (Sub Domain)

Situation:
The form is at https://example.com
If user write "demo" in input then submit, they will be redirected to
https://demo.example.com where "demo" is from the value they've putted before.
How to do that?
My code:
<form class="form-inline" action="http://example.com" method="GET">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Login</label>
<div class="input-group">
<div class="input-group-addon">https://</div>
<input type="text" class="form-control" id="exampleInputAmount" placeholder="username" value="">
<div class="input-group-addon">.example.com</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
If jQuery is ok for you, here is a simple solution:
$(function() {
$('#form').submit(function() {
var subdomain = $('#exampleInputAmount').val();
$('#form').attr('action', 'http://' + subdomain + '.example.com');
return true;
});
});
After button clicked the form will be submitted. So on the top of page you may use below code.
PHP Solution:
if(isset($_GET['submit'])){
$text = $_GET['inputtext'];
$url = 'http://'.$text.'example.com';
header('Location: ' . $url);
exit();
}
HTML Change:
<input type="text" name="inputtext" class="form-control" id="exampleInputAmount" placeholder="username" value="">
<button name="submit" type="submit" class="btn btn-primary">Login</button>

Variable not passing in POST method to same page

It seems that variables are not being passed to same page using POST method... I'm a beginner in html,php...
Please help...
table-name = member
attribute = name
<?php
include 'connection.php';
if(isset($_POST['submit'])) {
$name = $_POST{'name'};
$query = "INSERT INTO member (`name`) VALUES ('$name')";
mysqli_query($conn, $query);
header('Location: register.php');
}
?>
<form class="form-horizontal" action="register.php" method="post" id="input">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" value="" />
</div>
</div><br>
<input type="submit" name="submit" form="input" class="btn btn-primary" value="Register" />
</form>
<?php
/* $_POST is super global array you can check weather you are getting data from post or not by using below line of code and use square brakets for array
instead of { } */
echo '<pre>';
print_r($_POST);
echo '</pre>';
include 'connection.php';
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$query = "INSERT INTO member (`name`) VALUES ('$name')";
mysqli_query($conn, $query);
header('Location: register.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="register.php" class="form-horizontal" id="input" method=
"post" name="input">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input class="form-control" name="name" type="text" value="">
</div>
</div><br>
<input class="btn btn-primary" form="input" name="submit" type="submit"
value="Register">
</form>
</body>
</html>
First of all try by change third line
$name = $_POST{'name'};
to
$name = $_POST['name'];
Because in post we need to use square braces...
and if you are posting to same page then empty action attribute value, bcz empty action attribute means form will post to same page...
so form element will be as below...
<form class="form-horizontal" action="" method="post" id="input">
Let me know if it help or not...
Please Try this :
$name = $_POST{'name'}; to $name = $_POST['name'];
and
Remove form action : action="register.php" to action=""
use this code instead of your code..
<form class="form-horizontal" action="" method="post" id="input">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" value="" />
</div>
</div><br>
<input type="submit" name="submit" class="btn btn-primary" value="Register" />
</form>
Replace This Code With Your Existing Code
<?php
include 'connection.php';
if(isset($_POST['name'])) {
$name = $_POST['name'];
$query = "INSERT INTO member(name) VALUES ('".$name."')";
mysqli_query($conn, $query);
}
?>
<form class="form-horizontal" action="register.php" method="post" id="input">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" value="" />
</div>
</div><br>
<input type="submit" name="submit" form="input" class="btn btn-primary" value="Register" />
</form>
Also,
Please confirm $conn is not false
If your HTML code and PHP code both on same page then set form action=''
<form class="form-horizontal" action="" method="post" id="input">
Always check mysqli_error() and mysqli_connect_errno() in your query code.
also use exit; after header("Location: YOUR_URL");
Your PHP code should be as below:-
include 'connection.php';
if ( isset( $_POST['submit'] ) ) {
// You have wrongly used bracket. Use [] bracket instead of {}
$name = $_POST['name'];
/* check connection */
if ( mysqli_connect_errno() ) {
printf( "Connect failed: %s\n", mysqli_connect_error() );
exit();
}
$query = "INSERT INTO member(`name`) VALUES('$name')";
// Check your query successfully executed or not
$result = mysqli_query( $conn, $query ) or die( mysqli_error( $conn ) );
header( 'Location: register.php' ); /* Redirect browser */
exit; // Write exit after header
/* Make sure that code below does not get executed when redirect. */
}
Hope it will help you :)
Change
if(isset($_POST['submit']))
to
if(isset($_POST['name']))

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

Categories