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"> |
Freddz |
Jan 12 2019, 04:29 AM
Post
#2
|
Novice Group: Members Posts: 25 Joined: 7-January 19 Member No.: 26,791 |
Hello Christian.
Thank you again. But I still don't understand e.g. why there is a button in the else part. Etc. To show you the situation I put your first and afterwards your second php code part into a page into its (empty) form tags. So the second button 'Abschicken' is its send button (with Name='Absenden'). Now I can press both buttons in each case but nothing happens - like I expected. These are the screenshots: http://prntscr.com/m66io7 and http://prntscr.com/m66ayq. As you can see it's not working as you intended me to demonstrate. I am sorry, but would you please explain more detailed so that I can integrate your test code into a page properly? Thank you very much. This post has been edited by Freddz: Jan 12 2019, 04:45 AM |
Christian J |
Jan 12 2019, 05:55 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
But I still don't understand e.g. why there is a button in the else part. Etc. The database query should only run if a form has been submitted, so the first thing the PHP script does is check that. In pseudo-code: CODE if(the form was submitted) { Run the database query (and maybe a confirmation message for the user). Show no HTML form. } If no form has yet been submitted (such as the first time the user loads the page), the PHP script instead displays the form. In pseudo-code: CODE else { The form is not yet submitted, so the HTML form is shown on the page. } (It may seem counter-intuitive to use the ELSE condition the first time the page is loaded, but it's a more convenient way to write the PHP script.) QUOTE To show you the situation I put your first and afterwards your second php code part into a page into its (empty) form tags. So the second button 'Abschicken' is its send button (with Name='Absenden'). Now I can press both buttons in each case but nothing happens - like I expected. These are the screenshots: http://prntscr.com/m66io7 Not sure I understood the above, but you don't need two submit buttons. Just make sure the button's NAME attribute value is the same as in the $_POST variable. QUOTE This looks like a PHP syntax error, the PHP code is not supposed to appear on the page. Can you post the actual code you used here on the forum? |
Freddz |
Jan 12 2019, 09:34 AM
Post
#4
|
Novice Group: Members Posts: 25 Joined: 7-January 19 Member No.: 26,791 |
Hello Christian.
I think you think that my form page is described by php code. But it is not. It is pure HTML code only. And I just put your two code samples from above into the form page. That's it. Let's give you a simple example of my form page with just one select element and your first code sample (which does not contain syntax errors): ++++++++++++++++++++++++++++++++++++ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> </HEAD> <BODY NOF="(MB=(DefaultMasterBorder, 150, 51, 66, 0), L=(KontaktLayout, 849, 452))"> <DIV ID="LayoutLYR"><LAYER ID="LayoutLYR" TOP=0 LEFT=0 VISIBILITY=INHERIT WIDTH=915 HEIGHT=653 Z-INDEX=1><DIV ID="Text39LYR" CLASS="TextObject"><LAYER ID="Text39LYR" VISIBILITY=INHERIT TOP=190 LEFT=81 WIDTH=801 HEIGHT=38 Z-INDEX=1> </LAYER></DIV><DIV ID="Tabelle4LYR" STYLE="background-color: rgb(217,217,217);"><LAYER ID="Tabelle4LYR" VISIBILITY=INHERIT TOP=249 LEFT=104 WIDTH=746 HEIGHT=272 Z-INDEX=2 bgcolor="#D9D9D9"> <FORM NAME="Tabelle4FORMULAR" METHOD=POST> <TABLE ID="Tabelle4" BORDER=0 BGCOLOR="#D9D9D9" CELLSPACING=3 CELLPADDING=1 WIDTH="100%"> <?php if(isset($_POST['Absenden'])) { // put form handler script here echo "Form was submitted."; } else { ?> Form is not submitted. <form method="post"> <INPUT TYPE=SUBMIT NAME="Absenden"> </form> <?php } ?> <TR> <TD> <P> </P> </TD> <TD WIDTH=136> <P><B>Thema:</B></P> </TD> <TD WIDTH=253> <P> <SELECT ID="Auswahlfeld1" NAME="Auswahlfeld1" required> <OPTION VALUE="" SELECTED>/ Thema wählen /</OPTION> <OPTION VALUE="1">Option 1</OPTION> <OPTION VALUE="2">Option 2</OPTION> <OPTION VALUE="3">Option 3</OPTION> <OPTION VALUE="4">Option 4</OPTION> <OPTION VALUE="5">Option 5</OPTION> </SELECT> </P> </TD> <TD WIDTH=299> </TR> <TR> <TD> <P> </P> </TD> <TD> <P> </P> </TD> <TD WIDTH=253> <P STYLE="text-align: center;"><INPUT TYPE=SUBMIT NAME="Absenden" VALUE="Abschicken" ID="Schaltflaeche1"></P> </TD> <TD> <P> </P> </TD> <TD> <P> </P> </TD> </TR> </TABLE> </FORM> </BODY> </HTML> +++++++++++++++++++++++++++++++ Goal is again: If select box is not selected you get a notification (works fine already) and 'else' option should be taken. If it is selected the 'if' option should be taken which will contain the php code script later for saving the select box value to database. |
Christian J |
Jan 12 2019, 02:14 PM
Post
#5
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
Let's give you a simple example of my form page with just one select element and your first code sample (which does not contain syntax errors): You can't nest forms inside each other. Instead, put all of your existing HTML form in the ELSE: CODE <?php if(isset($_POST['Absenden'])) { // Put form handler script here. echo '<p>The form was submitted.</p>'; } else // The form is not yet submitted { ?> <FORM NAME="Tabelle4FORMULAR" METHOD=POST> ... </FORM> <?php } ?> As a sidenote, the LAYER element is ancient. It was only supported by the Netscape4 browser back in the 1990s, and is probably ignored by all other browsers. See also http://www.blooberry.com/indexdot/html/tagpages/l/layer.htm |
Freddz |
Jan 14 2019, 07:48 AM
Post
#6
|
Novice Group: Members Posts: 25 Joined: 7-January 19 Member No.: 26,791 |
Hello Christian.
Thank you very much. But once again your code begins with <?php again. I don't have a php file of the page. My page file has the extension .html, and not .php. So could you please solve with me this situation by keeping my main structure like sent at last? This was the main condition of my request. Maybe then I will have to use Ajax to execute the dbinsert php code and will have to use the event 'onSubmit'. I mentioned this in my very first message. Why shouldn't this be possible as well?... |
Christian J |
Jan 14 2019, 09:53 AM
Post
#7
|
. Group: WDG Moderators Posts: 9,722 Joined: 10-August 06 Member No.: 7 |
But once again your code begins with <?php again. I don't have a php file of the page. My page file has the extension .html, and not .php. If you don't want to change the file extension, you can easily configure your server to run PHP in files with .html extensions too. Usually this is done with a .htaccess directive, a few are mentioned here: https://stackoverflow.com/questions/4687208...un-as-php-files --but, since your current path is "../cgi-bin/DBinsert.php" maybe you currently only runs PHP as CGI? Ask your web host support what applies in your case. You might also load the form and PHP script page from "../cgi-bin/DBinsert.php" in an IFRAME in the .html parent page. QUOTE Maybe then I will have to use Ajax to execute the dbinsert php code and will have to use the event 'onSubmit'. I mentioned this in my very first message. Why shouldn't this be possible as well?... Yes Ajax can also be used, forum member CharlesEF is better at that than me. Personally I think Ajax is an unnecessary complication for this, though. |
Lo-Fi Version | Time is now: 21st September 2024 - 07:22 AM |