PHP form does not save

Asked

Viewed 56 times

0

I have this html form:

<form id="addRunner" name="addRunner" action="service.php" method="POST">
First Name: <input type="text" name="txtFirstName" id="txtFirstName" value="" placeholder="Firt Name"><br>
Last Name: <input type="text" name="txtLastName" id="txtLastName" value="" placeholder="Last Name"><br>
Gender: <select name="ddlGender" id="ddlGender">
         <option value="">--Please Select--</option>
         <option value="">Female</option>
         <option value="">Male</option>
</select><br>
Finish Time: <input type="text" name="txtMinutes" id="txtMinutes" value="" placeholder="(minutes)" maxlength="2">
<input type="text" name="txtSeconds" id="txtSeconds" size="10" maxlength="2" value="txtSeconds" placeholder="(seconds)">
<br><br>
<button type="submit" name="btnSave" id="btnSave">Add Runner</button>
<input type="hidden" name="action[]" id="action[]" value="addRunner">
</form>

This PHP code should do the validation and saving on the basis of the information sent by Form, but nothing is written in Base. Someone can help me?

$a_html = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_SPECIAL_CHARS);

if ($a_html === 'addRunner') {

    $fname = htmlspecialchars(isset($_POST['txtFirstName']) ? $_POST['txtFirstName'] : 'txtFirstName');
    $lname = htmlspecialchars(isset($_POST['txtLastName']) ? $_POST['txtLastName'] : 'txtLastName');
    $gender = htmlspecialchars(isset($_POST['ddlGender']) ? $_POST['ddlGender'] : 'Valor Padrão');
    $minutes = htmlspecialchars(isset($_POST['txtMinutes']) ? $_POST['txtMinutes'] : 'ddlGender');
    $seconds = htmlspecialchars(isset($_POST['txtSeconds']) ? $_POST['txtSeconds'] : 'txtSeconds');
    $time = $minutes . ':' . $seconds;

    if (preg_match('/[^\w\s]/i', $fname) || preg_match('/[^\w\s]/i', $lname)) {
        echo 'Invalid name provided';
    }

    if (empty($gender)) {
        echo 'Gender select a please';
    }

    $string_sql = "INSERT INTO testemunho (first_name, last_name, gender, finish_time) VALUES (null,'{$fname}','{$lname}','{$gender}','{$time}')";

    $result = mysqli_query($conect, $string_sql);

    if ($result) {
        echo 'Runners: ', "$fname","$lname" . "Added Sucess";
    } else {
        echo 'Erro ao gravar dados';
    }
}
  • Use a script called Jquerymask that will help you more than filter fields in PHP. You probably have some error in these PH filters that are preventing you from recording.

2 answers

0

You’ll need to debug your code better to understand which part of the code is generating an unwanted output:

Consider using the php function var_dump.

Here are some tips: How to debug code in PHP?

And see what the output of the variables: $result, $string_sql,$a_html

0


  1. Square brackets treat elements of the same name as an array that is not your case as it only has an Hidden input with name="action[]". Change that name to action without the clasps so that filter_input work correctly and the script enters if ($a_html === 'addRunner') {

  2. Options values are empty

     <option value="">Female</option>
     <option value="">Male</option>
    

    so you’ll always fall for that if

    if (empty($gender)) {
       echo 'Gender select a please';
    } 
    

    although this does not cause errors, put values in values like f and m for example.

Done this your INSERT should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.