Reputation: 5
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.
Upvotes: 0
Views: 104
Reputation: 3757
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>
Upvotes: 0
Reputation: 6893
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.
Upvotes: 1
Reputation: 348
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"> `
Upvotes: 0
Reputation: 15726
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
}
Upvotes: 0