I have this form, when I press the submit button i will get redirected to select.inc.php.
But sometimes I dont want to fill out every field of the form but all the "variables"(?) are still getting appended to the URL.
When I only enter an id and will press the button the url will look like this: localhost/inc/select.inc.php?id=1?username=?email=?password=?rank=.
What I want is this: localhost/inc/select.inc.php?id=1
<form action="inc/select.inc.php" method="GET">
<h1 id="h1-select">Select</h1>
<div class="row">
<input type="text" name="id" placeholder="Id">
</div>
<div class="row">
<input type="text" name="username" placeholder="Username">
</div>
<div class="row">
<input type="text" name="email" placeholder="E-Mail">
</div>
<div class="row">
<input type="password" name="password" placeholder="Password">
</div>
<div class="row">
<input type="text" name="rank" placeholder="Rank">
</div>
<div class="row">
<button type="submit" name="submit">Select</button>
</div>
</form>
I want this to show stuff from the MySQL database on a page like when I want to list all users with the rank "1".
If you want to remove all empty strings (you get them as empty strings, not null), you can run $_GET through array_filter():
$_GET = array_filter($_GET, function ($val) {
return $val !== '';
});
Now the $_GET-param will only contain non empty strings.
If you don't need to keep 0 either, you can just do:
$_GET = array_filter($_GET);
That removes any param where the value is falsy
Related
I have the file "1.php" and the file "2.php" ...
In "1.php", there's a HTML form:
<form action="2.php" method="post">
<div class="form-group">
<label for="db_username">Field 1:</label>
<input type="text" class="form-control" id="db_username">
</div>
<div class="form-group">
<label for="db_password">Field 2:</label>
<input type="password" class="form-control" id="db_password">
</div>
<div class="form-group">
<label for="db_passphrase">Field 3:</label>
<input type="text" class="form-control" id="db_passphrase">
</div>
<button type="submit" class="btn btn-default">Next</button>
</form>
While in "2.php", the action must be applied in PHP .. like that:
<?php
// Process of Step "1"
$db_username = $_POST['db_username'];
$db_password = $_POST['db_password'];
$db_passphrase = $_POST['db_passphrase'];
if( !isset($db_username) || !isset($db_password) || !isset($db_passphrase) ) {
header("Location: 1.php?error=1");
die();
}
?>
However .. the values of $_POST['db_username'], $_POST['db_password'] and $_POST['db_passphrase'] is empty...
You need to use the name attribute to later access the $_POST data, as in:
<input type="text" class="form-control" id="db_username" name="db_username">
You need to give the inputs a name and acces that value through $_POST['name'].
id is not relevant for php form handling
You have to give name for the textfields like this
<input type="text" class="form-control" id="db_username" name="db_username">
Put name instead of id. it should work
I'm currently encountering a problem which is baffling me. There will be a lack of PHP mixed with HTML due to using a MVC Framework.
What i've got:
Form A on Page 1 with a submit button which directs user input from Form A to page 2 with a chunk of text for the user to read before continuing, a hidden text field which will contain a serialized array from Form A and a submit button to continue to the validation of user input.
My forms direct to the correct pages as required, the post array can be seralized providing I do not put it into a text field. The HTML from form A follows:
<form action="/RegisterInformation" method="POST">
<div class="row">
<div class="large-12 columns">
<input type="text" name="Username" placeholder="Username, ex: JohnDoe ">
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="password" name="Password" placeholder="Password">
</div>
<div class="large-4 columns">
<input type="password" name="cpassword" placeholder="Confirm Password">
</div>
<div class="large-4 columns">
<input type="text" name="email" placeholder="Email, ex: JohnDoe#provider.com">
</div>
</div>
<div class="row">
<div class="large-12 columns">
<textarea name="Referral" placeholder="Where Did You Hear About Us?"></textarea>
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="submit" name="submit" value="Continue" class="button">
</div>
</div>
</form>
The redirect to page 2:
<div class="row">
<div class=" large-12 columns">
<p>
<?php echo $data['PreregisterInformation']; ?>
</p>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<form action="/Continue" method="POST">
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
<input type="submit" name="submit" value="Continue" class="button">
</form>
</div>
</div>
With the current example, i've got the text field set to being visible, so I can see what's going on.. But this is where it gets strange.
What i'm getting is two different types of results.
The first, is that when I echo the serialized array outside a input field. I get the desired results:
a:6:{s:8:"Username";s:1:"s";s:8:"Password";s:1:"4";s:9:"cpassword";s:1:"4";s:5:"email";s:3:"asd";s:8:"Referral";s:3:"asd";s:6:"submit";s:8:"Continue";}
The second is when I use the following:
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
Which passes an invalid serialized array to page 3 breaking the whole process.
I assume it's not a HTML related error, but the new line break in the serialized array definition a:6. I have made an attempt to rectify this problem by removing the new line break with str_replace:
$Ser = serialize($_POST);
$Ser = nl2br($Ser);
$Ser = str_replace("<br>","00",$Ser);
echo $Ser;
which has proven not to work.
This is not a direct solution of your problem, but have you tried json_encode or avoiding the hidden field by storing the date into $_SESSION?
let's say that I have a form
<form >
<div class="control-group">
<label class="control-label" for="numeinculpat">Nume inculpat</label>
<div class="controls">
<input type="text" id="numeinculpat" placeholder="Nume inculpat" name="nume" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="numeinculpat2">Nume inculpat2</label>
<div class="controls">
<input type="text" id="numeinculpat2" placeholder="Nume inculpat2" name="nume" />
</div>
</div>
</form>
And the user introduce some empty value .
How cand can I do if someone enter wrong data(or empty),when enter again, the input to have the value of data that he previous introduced ??
Add a value attribute and get last value from $_POST like below
<input
type="text"
id="numeinculpat2"
placeholder="Nume inculpat2"
value="<?php echo (isset($_POST['nume']) ? $_POST['nume'] : '' ); ?>"
name="nume"
/>
Input name must be unique, don't use same nume for other input, This might help
You are missing the action and the method attribute of form.
Follow the basic tutorial from here.
http://www.w3schools.com/php/php_form_complete.asp
If you are submitting it to the same page use " method="post">. Now when you submit and if the value is blank or whatever is the problem you will get the last entered value also use this to get the value.
if(isset($_POST["submit"])
{
$text1=$_POST["numeinculpat"];
}
over your html input
<div class="control-group">
<label class="control-label" for="numeinculpat">Nume inculpat</label>
<div class="controls">
<input type="text" id="numeinculpat" placeholder="Nume inculpat" name="nume" value="<?php echo $text1 ?>" />
</div>
</div>
Now when you submit the value you will get the last entered value.
I'm making a tool to pull eve API data and I use a GET form to take the data and get access to the key ID and code to access the data. This then pulls the first character ID on the key and grabs all that character data.
for example the url is:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL
I pull these variables out via the session variables that they create.
What I'm now trying to do is now have a for loop which puts up buttons for each character on the key so you can flick between characters, I'm trying and have tried get and post methods hidden in the button but can't seem to get my desired result. It seems to not be taking out the variable that i'm trying to pass and adding it into the $_SESSION array or the $_POST or the $_GET - i've tried print_r on all these variables but I can't get any output.
Here is my primary form thats on my main page:
<form method="get" action="APIChecker/" >
<legend> Submit an API Key</legend>
<div class="control-group">
<label class="control-label">Key ID:</label>
<div class="controls">
<input type="text"
class="input-small"
name="keyID""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Verification Code:</label>
<div class="controls">
<input type="text"
class="input-xxlarge"
name="vCode"
/>
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
What I'm trying to do now is get another session variable from another page - which updates a variable and refreshes the page with the keyID and code unaltered.
As in:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL **&charID=654321**
so $_SESSION['keyID'] = 123456
& $_SESSION['code'] = ABCDEFGHIJKL
& $_SESSION['charID'] = 654321
what I've tried is:
<form method="get" action="" >
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">
<input type="hidden"
name="charID"
value="$charID"
/>
</button>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
</form>
You can't output PHP values directly in HTML like this: value="$charID"
Try this instead:
value="<?php echo $charID; ?>"
I have the following code in index.php:
<div class="done">
<b>Thank you<?php
echo $_SESSION['session_vname']." ";
echo $_SESSION['session_lname']."! </b><br><br>Email: ";
echo $_SESSION['session_email']."<br> Status: ";
echo $_SESSION['session_status']."<br>";
?>
</div>
and
<div class="form">
<form method="post" action="process.php">
<div class="element">
<label>First Name</label>
<input type="text" name="vname" class="text" />
</div>
<div class="element">
<label>Last Name</label>
<input type="text" name="lname" class="text" />
</div>
<div class="element">
<label>Email</label>
<input type="text" name="email" class="text" />
</div>
<div class="element">
<label>Status</label>
<input type="text" name="status" class="text" />
</div>
<div class="element">
<input type="submit" id="submit"/>
<div class="loading"></div>
</div>
</form>
</div>
I execute the form with an ajax submit so the index.php gets no refresh. When finished
gets a fadeOut and a fadeIn.
This works fine except for the following part:
When div class="form" is shown, it shows the session variable from the form before the last submit.
Example:
first input: session_vname = testa
first output $_SESSION['session_vname'] = nothing
second input: session_vname = overflow
second output $_SESSION['session_vname'] = testa
So finally the question:
How can I force the to update the session variables after form submitting also index.php is not reloaded?
PHP code is executed on server side, before the page is shown to the viewer. That means you need to update static elements with JavaScript if you don't want to refresh the whole page.
You cant update server side php sessions using javascript, you should be using cookies for that type of functionality