Validating 'required' form page data for save to database, Saving by php code file after succesful validation. Success message on |
Validating 'required' form page data for save to database, Saving by php code file after succesful validation. Success message on |
Freddz |
Jan 7 2019, 08:34 PM
Post
#1
|
Novice Group: Members Posts: 25 Joined: 7-January 19 Member No.: 26,791 |
Hello,
I have a form page where I check the mandatory inputs client-sided by the attribute 'required' (when clicking send button). When all required inputs are done the send button click... 1.) should call a php file 'dbinsert.php' which just contains the save process of the form data to a database. 2.) But the success message should be shown on the SAME page, on top of the form page. How can I implement this process under those 2 conditions? I suppose that the send button's 'action' attrbute should be empty and the php file has to be called via Ajax (within a Javascript function?) !? But unfortunately I fail in implementing THIS specific configuration. Could you help me, please? If so, please specify in your answer all necessary code lines like the form header, the submit button and the Javascript function that executes the Ajax call (and also fires the success message, I suppose)? Thank you so much! The following constellation does not work: * <FORM NAME="Betrugseingabe" ACTION="../cgi-bin/DBinsert.php" METHOD=POST> * <INPUT TYPE="submit" NAME="Absenden" VALUE="Absenden" ID="Schaltflaeche1" formtarget="_self"> |
CharlesEF |
Jan 15 2019, 03:53 PM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I forgot to say that you didn't post enough 'DBinsert' code for me to tell if you will get a response back from the ajax call. Also, in the 'DBinsert.php' file you should check to make sure every value needed is supplied in the $_POST array before doing the database insert.
|
Freddz |
Feb 12 2019, 11:58 AM
Post
#3
|
Novice Group: Members Posts: 25 Joined: 7-January 19 Member No.: 26,791 |
I forgot to say that you didn't post enough 'DBinsert' code for me to tell if you will get a response back from the ajax call. Also, in the 'DBinsert.php' file you should check to make sure every value needed is supplied in the $_POST array before doing the database insert. Hello CharlesEF, I meanwhile tested your Ajax code (s. #33) in detail. And now I realised that a new recordset is created but the data are NOT inserted into the database. So it seems as if I still have to modify s.th. at my dbinsert.php file for this Ajax usage although it works fine when used for a usual access via HTML. However, I am sure that all the $_POST values which are needed for the SQL insert command are appended in the array handed over. Here is the whole code of dbinsert.php for you: +++++++++++++++++++++++++++++++++++++++ $servername = "localhost"; $username = "xyz"; $password = "abc"; $dbname = "name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO Interessenten (Name, Email, Telefon, ....) VALUES ('" . $_POST["Name"] . "', '" . $_POST["Mail"] . "', '" . $_POST["Telefon"] . "', '" . ..... "', '" . date("Y-m-d") . "')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> +++++++++++++++++++++++++++++++++++++++ So do you see the reason why the sql insert command is still not successful? (Remark: I suppose that the order of data elements in the appended string must not be the same than the one in the sql command...) This is the append code: var formData = new FormData(); formData.append(document.getElementsByName("Name").id, document.getElementsByName("Name").value); formData.append(document.getElementsByName("Mail").id, document.getElementsByName("Mail").value); formData.append(document.getElementsByName("Telefon").id, document.getElementsByName("Telefon").value); ... By the way how could I debug this process so that I would get a useful error message of this fault?... |
Lo-Fi Version | Time is now: 21st September 2024 - 03:10 AM |