Input value equal with previous enter value - php

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.

Related

Remove null variables from url

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

Duplicate Form using button

I'm trying to duplicate 4 input text. For example I want to duplicate 'Produk Simpanan Saham', then only 4 input on 'Produk Simpanan Saham' div will be duplicated. I already tried a few methods using jQuery but it seem doesn't work.
<form action="" method="post" class="form-horizontal">
<br><br>
<div class="form-group">
Upload File :
<input type="file" name="file">
</div>
<div class="saham">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Saham</center>
</h3>
</div>
<label for="kp_produk">Kode Produk :</label>
<input type="text" name="kp_produk" class="form-control">
<br>
<label for="produk_saham">Produk :</label>
<input type="text" name="produk_saham" class="form-control">
<br>
<label for="bunga_saham">Bunga :</label>
<input type="text" name="bunga_saham" class="form-control">
<br>
<label for="ket_saham">Keterangan :</label>
<input type="text" name="ket_saham" class="form-control">
</div>
<div class="harian">
<div class="w3-container w3-black">
<h3>
<center>Produk Simpanan Harian</center>
</h3>
</div>
<label for="kp_harian">Kode Produk :</label>
<input type="text" name="kp_harian" class="form-control">
<br>
<label for="produk_harian">Produk :</label>
<input type="text" name="produk_harian" class="form-control">
<br>
<label for="bunga_harian">Bunga :</label>
<input type="text" name="bunga_harian" class="form-control">
<br>
<label for="ket_harian">Keterangan :</label>
<input type="text" name="ket_harian" class="form-control">
</div>
you mush have blueprint of field first like
var blueprint= '<div class="saham">'+
'<div class="w3-container w3-black">'+
'<h3>'+
'<center>Produk Simpanan Saham</center>'+
'</h3>'+
'</div>'+
'<label for="kp_produk">Kode Produk :</label>'+
'<input type="text" name="kp_produk[]" class="form-control">'+
'<br>'+
......
used [] for all name field to get array request when you send form to server
example :
'<input type="text" name="kp_produk[]" class="form-control">'+
and make button add like (jquery) example
<button onclick="$('form').append(blueprint)">add</button>
just use
var dom=$($('.saham')[0]).clone();
//copy dom
$('.form-horizontal').html(dom):
//paste dom
dont forget to add [] on name tag dom input to set data send to array, because if you not set. data with key kp_produk value is last tag name with name kp_produk
if you use [] (kp_product[]) data will set to array
kp_product=['anu1',anu2];

PHP not getting any response from the form on the previous page

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

clearing the textbox input on button click

i do have multiple text boxes called first name,last name,phone no and so on.. and i want to clear/refresh the input typed in it on button click called as refresh or clear.the problem is that the input in these text boxes are not getting refreshed because the data in it is coming from other page and it is getting refreshed when we type some thing in it
my code is as follows:
<div class="control-group">
<input value="<?php echo $model['pros']['oppid'] ?>" name="oppid" type="hidden" id="prospectid" />
<label class="control-label">First Name </label>
<div class="controls">
<input type="text" name="f_name" pattern="[-a-zA-Z]+" required="" title="First Name is required" value="<?php echo $model['pros']["f_name"]; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Last Name </label>
<div class="controls">
<input type="text" name="l_name" pattern="[-a-zA-Z]+" required="" title="Last Name is required" value="<?php echo $model['pros']["l_name"];?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Primary phone </label>
<div class="controls">
<input type="tel" required="" pattern="[-0-9]+" title="Please Enter correct phone" name="pri_phone" value="<?php echo $model['pros']["pri_phone"];?>" />
</div>
</div>
and the button as
<button class="btn">New</button>
please suggest me on this...
use the following code:
$("#elementid").live('click',function(){
$(this).parents('form').find('input[type="text"]').val('');
});
Assuming that your input elements are inside of a form, and so too is your button, you can do this using jQuery:
$('button.btn').click(function(e) {
e.preventDefault();
$(this).parents('form').find('input[type="text"], input[type="tel"]').val('');
});
you can write code in jquery as:
$('#new').live('click',function(){
$('#fname').val('');
$('#lname').val('');
$('#tel').val('');
});
If your button is not within the form you can use the following
$('button.btn').click(function() {
$('.control-group input:not(:submit)').val('');
});
This will reset any type of input within the control-group divs except for submit inputs
If your reset button is within the form then BenM's answer is what you want.
$(".clearform").live('click',function(){
$(this).parents('form').find('input[type="text"], input[type="tel"],input[type="time"],input[type="email"],input[type="number"],input[type="date"],select').val('');
});

Update SESSION variable

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

Categories