I have a form that I want to submit and I check if any textbox has text so I can UPDATE something in a database.
This is the code for the form:
<form action="" method="POST"/>
CNP Nou: <input type="text" name="cnpN"/><br/>
Nume Nou: <input type="text" name="numeN"/><br/>
Prenume Nou: <input type="text" name="prenN"/><br/>
Data Nasterii Noua: <input type="text" name="dataNN"/> De forma AAAA-ZZ-LL <br/>
Sex Nou: <input type="text" name="sexN"/> F sau M <br/>
Numar Telefon Nou: <input type="text" name="telN"/><br/>
Adresa Noua: <input type="text" name="adrN"/><br/>
E-mail Nou: <input type="text" name="mailN"/><br/>
<input type="submit" value="Modifica" name="search2" class="submit" />
</form>
Then I check if the button is clicked so I can see if any textbox has text written in order to make an UPDATE in my database:
if (isset($_POST["search2"]))
{
if (!empty($_POST['cnpN']) || !empty($_POST['numeN']) || !empty($_POST['prenN']) || !empty($_POST['dataNN']) || !empty($_POST['sexN']) || !empty($_POST['telN']) || !empty($_POST['adrN']) || !empty($_POST['mailN']))
{
//php code for update
}
}
else
{
echo "<h4><b> Eroare! </b><h4>";
}
The problem is that without clicking the button I see the "Eroare!" message. If I remove that else statement and I click the button nothing happens to the database, even if I introduce something in the form.
I used the else statement just to see if that might be the problem or not.
I am looking through the code for some time and can't see the problem.
I know there are simpler ways to check the completed textboxes but I'm new to php and I thought it's easier this way.
The else clause belongs on the if not empty conditional.
When you first load the php script, there is no POST data present. That is expected since it is a GET request. This is why the initial conditional is false and the error message appears. POST will never be set on an HTTP GET request.
Submit button is not already clicked , but your code is outputting 'Eroare' because, initially, you don't have set any post data on page load, including search2. So you don't need else part of conditional unless you mark that an error occurred, but within the if(isset($_POST["search2"])){} block.
Otherwise, it will always output the 'Eroare'.
You first need to validate your form data, then to throw the error if any of form data doesn't fulfill the conditions.
About validation process, you would either need to implement some existing validation libraries or to extend your conditional to check for specific data, specific validation requirements.
Some of them would be required (not empty), some of them would require constrained/limited values from a list ( like gender field ), some would require number value validation ( phone ), an email field would require email value validation.
Also, you are missing the part for DB insertion.
Simplified code without advanced validation would be like this:
<?php
$post_search2 = filter_input(INPUT_POST,'search2'); //filter_input returns empty if input not set, and it is useful to filter and validate specific values;
if(!empty($post_search2))
{
$form_values = array('cnpN', 'numeN', 'prenN', 'dataNN', 'sexNN', 'telN', 'adrN', 'mailN'); //I have placed it in array to avoid having large code and simplify checks through iteration
$parsed_data = array();
foreach($form_values as $form_value){
$value = filter_input(INPUT_POST, $form_value);
if(!empty($value)){ //update parsed data only if form data is not empty
$parsed_data[$form_value] = $value;
}
}
//so if any of data is filled, do the updates
//this actually does same as !empty($_POST['cnpN']) || !empty($_POST['numeN']) || !empty($_POST['prenN']) && ...
// if you would require all data filled, check if count($parsed_data) === count($form_values) and that actually does same as this actually does same as !empty($_POST['cnpN']) && !empty($_POST['numeN']) && !empty($_POST['prenN']) && ...
//
if(count($parsed_data) > 0){
//php code for update
}else{
echo "<h4><b> Eroare! </b><h4>";
}
}
?>
<form action="" method="POST">
CNP Nou: <input type="text" name="cnpN"/><br/>
Nume Nou: <input type="text" name="numeN"/><br/>
Prenume Nou: <input type="text" name="prenN"/><br/>
Data Nasterii Noua: <input type="text" name="dataNN"/> De forma AAAA-ZZ-LL <br/>
Sex Nou: <input type="text" name="sexN"/> F sau M <br/>
Numar Telefon Nou: <input type="text" name="telN"/><br/>
Adresa Noua: <input type="text" name="adrN"/><br/>
E-mail Nou: <input type="text" name="mailN"/><br/>
<input type="submit" value="Modifica" name="search2" class="submit" />
</form>
Just remove the close tag from the form tag
ie, change <form action="" method="POST"/> to <form action="" method="POST">
If it doesn't solve your issue then use the following code snippet,
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
CNP Nou: <input type="text" name="cnpN"/><br/>
Nume Nou: <input type="text" name="numeN"/><br/>
Prenume Nou: <input type="text" name="prenN"/><br/>
Data Nasterii Noua: <input type="text" name="dataNN"/> De forma AAAA-ZZ-LL <br/>
Sex Nou: <input type="text" name="sexN"/> F sau M <br/>
Numar Telefon Nou: <input type="text" name="telN"/><br/>
Adresa Noua: <input type="text" name="adrN"/><br/>
E-mail Nou: <input type="text" name="mailN"/><br/>
<input type="submit" value="Modifica" name="search2" class="submit" />
</form>
PHP Code
if(isset($_POST["search2"]))
if(!empty($_POST['cnpN']) || !empty($_POST['numeN']) || !empty($_POST['prenN']) || !empty($_POST['dataNN']) || !empty($_POST['sexN']) || !empty($_POST['telN']) || !empty($_POST['adrN']) || !empty($_POST['mailN']))
{
//php code for update
}
Your code is working perfectly normal, output is showing because in the begining the "form" is not submitted and "if" it is not submitted then "else" should work and "else" is working. if you don't want it to be shown then you can remove else.it will work fine, also try not to end the form tag early
instead of
<form action="" method="POST"/>
use this
<form action="" method="POST"> `
Related
I have a html table where I need to take the users input safely and securely to update a table item. Any guidance? (I know what I've wrote below is incorrect)
For example if they wanted to update their own details for example surname:
<div class="grid-2">
<p><b>UPDATE MY DETAILS</b></p>
<form action ="includes/update.inc.php" method ="post">
<label>S.Name</label>
<input name="update-surname" type="text" placeholder="Enter new surname...">
<label>Address</label>
<input name="update-houseno" type="text" placeholder="Enter house no' or name...">
<input name="update-ln1" type="text" placeholder="1st Line of Address...">
<input name="update-town" type="text" placeholder="Town...">
<input name="update-county" type="text" placeholder="County...">
<input name="update-postcode" type="text" placeholder="Postcode...">
<label>Contact Number</label>
<input name="update-number" type="text" placeholder="Contact Number...">
<label>Email</label>
<input name="update-email" type="text" placeholder="Email...">
<input type="submit" name="update-details" value="Update">
</form>
</div>
Update name snapshot.....
UPDATE I have added code to the above page and an action on the form as requested. The code below is the start of what I've made to the page the action leads to:
<?php
// Here we check whether the user got to this page by clicking the proper button.
if (isset($_POST['update-details'])) {
require 'dbh.inc.php';
// We grab all the data which we passed from the update form so we can use it later.
$surname = $_POST['update-surname'];
$houseno = $_POST['update-houseno'];
$ln1 = $_POST['update-ln1'];
$town = $_POST['update-town'];
$county = $_POST['update-county'];
$postcode = $_POST['update-postcode'];
$number = $_POST['update-number'];
$email = $_POST['update-email'];
// We validate email is correct if email has been updated.
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../after-login.php?error=invalidmail&uid=");
exit();
}
}
?>
So you need an action on your form - i.e a script to point it to to process the form (can be the same script). You also ideally want to set the method to post so that data isn't visible in the url then you need to clean your data, connect to the db and do your query.
This should help you get the right idea
Form in PDO to update data
I didn't even know how to search for this, but I tried anyways and found nothing. If it's already answered, I give my apologize.
I have this form:
<form>
<div style="width:140px;float:left">Nro Socio: <input type="text" id="legajo" name="legajo" size="10" /></div>
<div style="width:200px;float:left">Nombres: <input type="text" id="nombre" name="nombre" size="22" /></div>
<div style="width:200px;float:left">Apellido: <input type="text" id="apellido" name="apellido" size="22" /></div>
<div style="width:50px;float:left"><input type="submit" id="srch" name="srch" value="Buscar" onclick="offen()" /></div>
</form>
Where offen() goes:
function offen()
{
$sid = document.getElementById('legajo').value;
$nom = document.getElementById('nombre').value;
$ap = document.getElementById('apellido').value;
if ($sid == "" && $nom == "" && $ap == "") alert ("No se ha ingresado ningún parametro de búsqueda");
else var wnd=window.open('../php/srchSoc.php?sid='+$sid+'&nom='+$nom+'&ap='+$ap,'mywindow',
'width=680,height=350,location=0,menubar=0,toolbar=0,location=0');
}
Why I do this instead of using target="_blank" or "_new"? Because I need this to be popup like, and I found no way to make it happen with the form itself.
The page srchSoc.php does some query, and if the resulting rows is just 1, automatically sets opener location to the same page, but with a $_GET value. i.e: '..site/bSoc.php turns' to '..site/bSoc.php?s=1'
Instead, I'm getting that '..site/bSoc.php' turns to '..site/bSoc.php?legajo=&nombre=&apellido=' Just like the form fields names.
Function where I set opener location on srchSoc.php goes like this:
<script>window.opener.location='../files/bSoc.php?soc=".$row['c_socio']."';
window.close();</script>
Where $row['c_socio'] is a column of the query result.
You need to prevent the normal form submission. Change the form submit button to:
<input type="submit" id="srch" name="srch" value="Buscar" onclick="offen(); return false;" />
return false prevents the submit button from submitting the form.
Why not simply using
<form method="get" action="fileyouwanttogo.php">
I'm trying to use POST to get values from a form. I moved the part out to a test page:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" value="enter something" />
</form>
// following that
<?php
if (isset($_POST["subject"]))
echo $_POST["subject"];
else
echo "input is not set";
?>
The echo is always "input is not set" regardless I set the value of input or not. And the tag "subject" does exist. This confused me. Why can't I get the value?
You should seperate your HTML form from your $_POST processing. This means that you provide the user a page which contains your posted form and then submit of this form is posted to a (different) page.
For example your form is (on form.php):
<form id="form" action="process_form.php" method="post">
<input type="text" name="subject" id="subject" value="" />
<input type="submit" value="Submit">
</form>
Then you create a file process_form.php:
<?php
if (isset($_POST['subject'])) {
echo $_POST['subject'];
}
else {
echo 'input is not set.';
}
?>
I have this issue with my validation and posting the data to another page.
Here is my form:
Signup.php
<form id="regForm" action="<?php echo htmlspecialchars($_SERVER["submit.php"]);?>" method="post" name="regForm">
<label for="fname">First Name:</label><input name="fname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['fname'])){echo $_POST['fname'];}?>"/><br/>
<label for="mdname">Middle initial:</label><input name="mdname" type="text" size="10" maxlength="35" value="<?php if(isset($_POST['mdname'])){echo $_POST['mdname'];}?>"/><br/>
<label for="lname">Last Name:</label><input name="lname" type="text" size="25" maxlength="35" value="<?php if(isset($_POST['lname'])){echo $_POST['lname'];}?>"/><br/>
<br/>
<label> </label><input type="submit" name="Signup" class="formButton" value="Signup" /></form>
And here is my submit.php which will validate the signup.html input
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
Now, my issue is this, in my "submit.php" file, I want to know what codes to put after the form fields validation that would enable me post the input data to another page because, I plan making it a two-page signup form. Let's say my next page is signup-2.html
how do I post the data after validation to the next page? I know how to retrieve the posted data on the next page like using Session or Echo the $_POST data but, my main issue is....how do I make the form post the data after the validation messages in my submit.php file?
use header :
header("Location: your page url?fname=$_POST['fname']&lname=$_POST['lname']");
but before this do not echo or print anything otherwise it won't redirect to that page.
you can use the data on destination page like this:
$_GET['fname']
example:
submit.php
function msg($status,$txt)
{
return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
// we check if everything is filled in and perform checks
//check if fname is empty
if(!$_POST['fname'])
{
die(msg(0,"<p>Please enter your first name.</p>"));
}
//check if lname is empty
if(!$_POST['lname'])
{
die(msg(0,"<p>Please enter your last name.</p>"));
}
header('Location:download.php?fname='.$_POST['fname']."&lname=".$_POST['lname']);
view.php
<html>
<body>
<form action="submit.php" method="post">
<input type='text' id="fname" name="fname"/>
<input type='text' id="lname" name="lname"/>
<input type="submit" id="button" value="submit"/>
</form>
</body>
</html>
download.php
<?php
echo "First Name........".$_GET['fname'];
put these three file in same directory and run view.php. you will be ok.
I am quite new to programming and am trying to set up a simple test form that is supposed to add or subtract the two numbers given. I have done it several times and it's always worked well. This one, I can't figure out what is wrong. When I hit submit, it reopens the same page in another tab.
Code for the form (html)
<form id="envio" target="formulario2p.php" method="POST">
Ingrese primer valor: <br>
<input type="text" name="valor1" id="valor1" />
<br>
Ingrese segundo valor: <br>
<input type="text" name="valor2" id="valor2" />
<br>
Indique operacion: <br>
<input type="radio" name="radio1" value="suma"/> sumar
<br>
<input type="radio" name="radio1" value="resta"/> restar
<br>
<input type="submit" value="operar"/>
</form>
PHP:
<?php
$valor1=$_POST["valor1"];
$valor2=$_POST['valor2'];
$radio1=$_POST["radio1"];
echo $valor1; echo $valor2;
if($radio1=="suma")
{$suma=$valor1 + $valor2;
echo "la suma es ".$suma;}
else {
$resta=$valor1 - $valor2;
echo "la resta es ".$resta;
}
?>
Specify action:
You are doing:
<form id="envio" target="formulario2p.php" method="POST">
Should be
<form id="envio" action="formulario2p.php" method="POST">
target attribute specifies where to open a new documents. http://www.w3schools.com/tags/att_a_target.asp
action specifies which url to send the form data to. See http://www.w3schools.com/tags/att_form_action.asp
You are missing action in your form. You may try:
<form id="envio" action="formulario2p.php" method="POST">
...
</form>