robsstevejobs Posted March 29, 2019 Share Posted March 29, 2019 hi everyone, is there anyone to help me, I created a database with PHPmyadmin with a registration form. to my surprise when I save a member the database assigned random incrementation and then it leaves empty box in database. what can I do? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 29, 2019 Share Posted March 29, 2019 Can you describe the situation with a little more detail? Link to comment Share on other sites More sharing options...
robsstevejobs Posted April 11, 2019 Author Share Posted April 11, 2019 ok, look at my code in html and PHP with the image of my database, it skips the line and it leaves empty box on the table. HTML CODE Form <form id="news" action="insert.php" method="POST"> <label for="name">Nom</label> <input type="text" name="name" placeholder="votre nom"> <Br> <br> <label for="email">E-mail</label> <input type="email" name="email" placeholder="votre mail"> <br> <br> <input type="submit" name="button" value="newsletters"> </form> PHP CODE This is insert.php file <?php $servername = "localhost"; $username = "root"; $password = "Robss123"; $dbname = "newsletter_database"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("connection failed:" .$conn->connect_error); } $name = mysqli_real_escape_string($conn, $_POST['name']); $email = mysqli_real_escape_string($conn, $_POST['email']); $sql = "INSERT INTO data_user_insert (name, email) VALUES ('$name','$email')"; if ($conn->query($sql) === TRUE) { echo "Votre enregistrement à reussi"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); if ($_SREVER["REQUEST_METHOD"] == "POST"){ $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); } function test_input ($data){ $data = trim($data); $data = stripcslashes($data); $data = htmlspecialchars($data); return $data; } ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 11, 2019 Share Posted April 11, 2019 It's probably inserting blank rows because you don't do any validation, you don't check if anything was submitted before inserting, you just insert. All of the code after you close the database connection isn't doing anything, you have a typo in it but the only thing you do is run a function on things that would be in post. You don't do anything after doing that though. You should check to see if the form was submitted before inserting data. Link to comment Share on other sites More sharing options...
robsstevejobs Posted April 15, 2019 Author Share Posted April 15, 2019 hi, what solution do you offer me in this area, what is the code I can use to check and then eliminate empty space in the database. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 15, 2019 Share Posted April 15, 2019 You need to check if the form is submitted. The last example on this page does that by checking the request method: https://www.w3schools.com/php7/php7_form_validation.asp That's not the only way to check if a form was submitted, but that's one way. Everything you want to do when the form is submitted needs to happen after you verify that it was actually submitted. Link to comment Share on other sites More sharing options...
robsstevejobs Posted April 16, 2019 Author Share Posted April 16, 2019 you are sure after validating the form empty space would no longer be in the database Link to comment Share on other sites More sharing options...
dsonesuk Posted April 16, 2019 Share Posted April 16, 2019 If you validate the form checking for empty required submitted fields IN PHP, that returns back to the form if it fails and only will be inserted into the database if it passes validation. Then Yes! no empty fields will appear in the database. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2019 Share Posted April 16, 2019 you are sure after validating the form empty space would no longer be in the database You're the programmer. If you don't want to insert empty rows in the database, then check if the data is empty before inserting. Doesn't that sound like it would prevent empty rows? Seriously, a programming language is just the way that we tell computers what to do. You're the one telling the computer what to do. If you don't want it to do something, then make it not do it. It really is that simple. Link to comment Share on other sites More sharing options...
robsstevejobs Posted April 19, 2019 Author Share Posted April 19, 2019 Hi guys, is it necessary to validate the form with PHP only, or I can also validate it with Jquery. Link to comment Share on other sites More sharing options...
dsonesuk Posted April 19, 2019 Share Posted April 19, 2019 Jquery is javascript which can be disabled by your browser. The best and safest option is to always validate using server language such as php before saving to database, then use jquery/javascript to validate client side before it gets submitted to give a quicker validation experience to the user without the need validate with php and having to reload and repopulating data entered to correct and continue when validation fails server side. Link to comment Share on other sites More sharing options...
robsstevejobs Posted April 19, 2019 Author Share Posted April 19, 2019 thank you guys, I'll come back and give you the result of my newsletters. Link to comment Share on other sites More sharing options...
robsstevejobs Posted May 15, 2019 Author Share Posted May 15, 2019 Hi thanks ! it works, but I have a problem running the page does not refresh. I used AJAX but it does not work Link to comment Share on other sites More sharing options...
justsomeguy Posted May 15, 2019 Share Posted May 15, 2019 If you want help here we need a clear description of the problem and the relevant code. Link to comment Share on other sites More sharing options...
robsstevejobs Posted May 16, 2019 Author Share Posted May 16, 2019 I have a problem with my form, I go when I execute the submit button, I stay in the same page without refreshing php page Link to comment Share on other sites More sharing options...
dsonesuk Posted May 16, 2019 Share Posted May 16, 2019 As only YOU! Have access to the code, I should think you did something wrong. Link to comment Share on other sites More sharing options...
robsstevejobs Posted May 16, 2019 Author Share Posted May 16, 2019 Look my code <form id="news" action="" method="POST"> <label for="name">Nom</label> <input type="text" name="name" id="name" placeholder="votre nom" required> <Br> <br> <label for="email">E-mail</label> <input type="email" name="email" id="email" placeholder="votre mail" required> <br> <br> <input type="submit" name="submit" id="submit" value="newsletters"> <span id="error_message" class="text-danger"></span> <span id="success_message" class="text-success"></span> </form> This is Ajax code $(document).ready(function(){ $('#submit').click(function(){ var name = $('#name').val(); var email = $('#email').val(); if(name == '' || email == '') { $('#error_message').html("All field are required"); } else { $('#error_message').html(''); $.ajax({ url:"validate.php", method:"POST", data:{name:name, email:email}, success:function(data){ $("#news").trigger("reset"); $('#success_message').fadeIn().html(data); setTimeout(function(){ $('#success_message').fadeOut("Slow"); }, 2000); } }); } }); }); </script> Link to comment Share on other sites More sharing options...
dsonesuk Posted May 16, 2019 Share Posted May 16, 2019 You need to prevent the form from doing its default action of submitting by using preventDefault(); https://www.w3schools.com/jquery/event_preventdefault.asp You should not use click event on the submit button but a onsubmit event on the form itself, as a form can be submitted by hitting enter key while within a input element. Link to comment Share on other sites More sharing options...
robsstevejobs Posted May 16, 2019 Author Share Posted May 16, 2019 but the url does not function on, i will save the data in the database, i notice the insert.php link does not work to save the information in the database Link to comment Share on other sites More sharing options...
dsonesuk Posted May 16, 2019 Share Posted May 16, 2019 in its present state the form will submit as usual and reload the form page, before it gets a chance to process for ajax call. Link to comment Share on other sites More sharing options...
robsstevejobs Posted May 16, 2019 Author Share Posted May 16, 2019 how to fix this problem, because i made the ajax call but still reload the page and then it does not insert the data into database Link to comment Share on other sites More sharing options...
justsomeguy Posted May 16, 2019 Share Posted May 16, 2019 You need to prevent the form from submitting normally. See the link above. Link to comment Share on other sites More sharing options...
robsstevejobs Posted May 16, 2019 Author Share Posted May 16, 2019 where the link above ?? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 16, 2019 Share Posted May 16, 2019 You need to prevent the form from doing its default action of submitting by using preventDefault(); https://www.w3schools.com/jquery/event_preventdefault.asp You should not use click event on the submit button but a onsubmit event on the form itself, as a form can be submitted by hitting enter key while within a input element. Link to comment Share on other sites More sharing options...
robsstevejobs Posted May 16, 2019 Author Share Posted May 16, 2019 thank you I try and then I will come back to you. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now