The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

3 Pages V  1 2 3 >  
Reply to this topicStart new topic
> Validating 'required' form page data for save to database, Saving by php code file after succesful validation. Success message on
Freddz
post 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">
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 8 2019, 05:56 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



Hello

QUOTE(Freddz @ Jan 8 2019, 02:34 AM) *

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?

The easiest way is to put the HTML form code and PHP script in the same file. Then if you omit the ACTION attribute of the FORM element, the form will submit to its own URL:

CODE
<FORM NAME="Betrugseingabe" METHOD=POST>

You must also write the PHP script so that it checks if the form has been submitted or not when the page is loaded. This can be done with the isset() method checking if any of the form variables have been set.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 8 2019, 08:55 AM
Post #3


Novice
**

Group: Members
Posts: 25
Joined: 7-January 19
Member No.: 26,791



Thank you very much.

But how is the button (<Input Type= ...>) been defined so that the PHP-Script is been executed.
JUST this part you unfortunately omited. ;-)
If I e.g. use:
"<INPUT TYPE=SUBMIT NAME="Absenden" onClick="return handleClick();>"
the attribute 'required' of the mandatory input elements will be ignored !!!...

Could you specify this button definition in detail, please?

This post has been edited by Freddz: Jan 8 2019, 09:45 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 8 2019, 10:10 AM
Post #4


Novice
**

Group: Members
Posts: 25
Joined: 7-January 19
Member No.: 26,791



Another little question to you related to my problem:
Isn't there anywhere a nice, simple, powerful, free WYSIWYG editor that supports things like that?
Do you know some?
I wish to create such web sites (including form pages like that) in a rapid way!...

This post has been edited by Freddz: Jan 8 2019, 10:24 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 8 2019, 12:39 PM
Post #5


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Freddz @ Jan 8 2019, 02:55 PM) *

Could you specify this button definition in detail, please?

Something like this:

CODE
<?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
}
?>

Sidenote:
QUOTE
I check the mandatory inputs client-sided by the attribute 'required'

Note that client-side form validation can easily be circumvented by malicious users, so always validate the form on the server-side as well.

The above PHP only checks if some POST form was submitted, it doesn't check if all form fields were included in the form submission, or if the data is valid or safe to use. To be on the safe side, run isset() on all the form fields, sanitize the data from all of them as well with e.g. htmlspecialchars(). Further checks might be required before creating an email or querying a database.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 8 2019, 12:42 PM
Post #6


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Freddz @ Jan 8 2019, 04:10 PM) *

Isn't there anywhere a nice, simple, powerful, free WYSIWYG editor that supports things like that?
Do you know some?

No idea, I never use WYSIWYG editors. A text editor with a clipbook library goes a long way though. You can also use templates that you've already created.



User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 10 2019, 04:57 AM
Post #7


Novice
**

Group: Members
Posts: 25
Joined: 7-January 19
Member No.: 26,791



Hello Christian.
I am very sorry but I do not understand your code:
You integrated the form description with button into a php script??

But how could the php script be started?? What from?
Cause it should be a click to the button only. But this button is within the php script.
So one process requires the other. How should that work?...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 10 2019, 07:35 AM
Post #8


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Freddz @ Jan 10 2019, 10:57 AM) *

But how could the php script be started?? What from?

PHP in a file runs every time the file is requested. Depending on wether the form has been submitted or not, your script can then be made to either save to the database or display the HTML form.

The isset() function checks if the form field "Absenden" has been submitted. If it has the IF condition becomes true, and the code in it will run:

CODE
if(isset($_POST['Absenden']))
{
    // here you can put the form handling script and database query
}

In other cases, the IF condition is false, and the ELSE condition will instead make the HTML form appear:

CODE
else
{
?>
Form is not submitted.

<form method="post">
<INPUT TYPE=SUBMIT NAME="Absenden">
</form>

<?php
}
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 10 2019, 07:41 AM
Post #9


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



Here's another way of looking at it, where especially the ELSE condition may be easier to understand:

CODE
<?php

echo '<p>This text is always shown when the page is loaded.</p>';

if(isset($_POST['Absenden']))
{
    echo '<p>This text is only shown if the form field "Absenden" was submitted.</p>';
}

else
{
    echo '<p>This text is only shown if the page was NOT requested by a POST form submission.</p>';
    echo '<form method="post"><input type="submit" name="Absenden" value="Submit \'Absenden\'"></form>';
}

?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 12 2019, 04:29 AM
Post #10


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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 12 2019, 05:55 AM
Post #11


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Freddz @ Jan 12 2019, 10:29 AM) *

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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 12 2019, 09:34 AM
Post #12


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>&nbsp;</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>&nbsp;</P>
</TD>
<TD>
<P>&nbsp;</P>
</TD>
<TD WIDTH=253>
<P STYLE="text-align: center;"><INPUT TYPE=SUBMIT NAME="Absenden" VALUE="Abschicken" ID="Schaltflaeche1"></P>
</TD>
<TD>
<P>&nbsp;</P>
</TD>
<TD>
<P>&nbsp;</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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 12 2019, 02:14 PM
Post #13


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Freddz @ Jan 12 2019, 03:34 PM) *

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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 14 2019, 07:48 AM
Post #14


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?...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 14 2019, 09:53 AM
Post #15


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Freddz @ Jan 14 2019, 01:48 PM) *

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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 14 2019, 11:54 AM
Post #16


Novice
**

Group: Members
Posts: 25
Joined: 7-January 19
Member No.: 26,791



So from your side you just cannot send me a solution which just keeps my basic structure?...:
A classic html page with client-verification = attribute 'required' which uses php ONLY for a db insert process?

I suppose Ajax is only necessary if I want to use/start a separate php file JUST for the db insert process (but not for building a whole page), isn't it?
But if I integrate this php code into an HTML form Ajax might even be avoided!?
So THIS solution I was asking for...

So you cannot help me here? But I should ask CharlesEF anyway?
Is your suggestion that I should write him a direct message?...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 14 2019, 12:52 PM
Post #17


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I posted a $_GET Ajax example a couple of weeks ago. See post #14 in this thread. If you need a $_POST Ajax example just post back.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 14 2019, 01:05 PM
Post #18


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Freddz @ Jan 14 2019, 05:54 PM) *

So from your side you just cannot send me a solution which just keeps my basic structure?...:
A classic html page with client-verification = attribute 'required' which uses php ONLY for a db insert process?

If you want to keep your structure it seems you must either load "../cgi-bin/DBinsert.php" in an iframe or call it with Ajax.

QUOTE
I suppose Ajax is only necessary if I want to use/start a separate php file JUST for the db insert process (but not for building a whole page), isn't it?

Yes, Ajax or an iframe.

QUOTE
But if I integrate this php code into an HTML form Ajax might even be avoided!?

Yes. For example, use this in the HTML form page:

CODE
<iframe name="foo"></iframe>
<FORM NAME="Betrugseingabe" ACTION="../cgi-bin/DBinsert.php" METHOD=POST target="foo">

If you also want a success message, "../cgi-bin/DBinsert.php" must create it as an HTML page in the iframe. But if you use Ajax instead of an iframe you may have greater control over where to place the success message.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Freddz
post Jan 14 2019, 01:58 PM
Post #19


Novice
**

Group: Members
Posts: 25
Joined: 7-January 19
Member No.: 26,791



I meanwhile think the basic question ist just a simple one here:
How can be detected that the 'required' attribute is fulfilled? (that any mandatory field is set) I suppose that there is a page submission as soon as 'required' is fulfilled from all the field elements. Isn't that correct??

So then the php script to make an db insert has to be started. That's it, isn't it?
So isset should not be really necessary!?...

Instead shouldn't e.g. the following architecture generally work then?...
+++++++++++++++++++++++++++++++++++++++++++
<script>
function DBinsert(){
<?php
echo "Form was submitted. Place the DBinsert code here!";
?>
alert("Field data saved successfully!...");
}
</script>

<form id="myForm" method="POST" action="" onSubmit="DBinsert();">

<select id="choice" name="choice[]" multiple required>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>

<input type="submit" name="btnsubmit">
</form>
+++++++++++++++++++++++++++++++++++++++++++
Action="" means that success message is not shown on another page. Instead a function is executed via onSubmit event which contains the DBinsert php script.
So why shouldn't that work fulfilling all my client-side conditions, Christian?...

Note: Of course you could use isset instead of onSubmit as well, replacing function DBinsert by isset's if condition!?...

Please let me know which mistake I do here.

This post has been edited by Freddz: Jan 14 2019, 02:06 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Jan 14 2019, 02:19 PM
Post #20


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



If you want to make sure your required fields are not blank before you submit the form then all you have to do is click the Submit button. The browser will alert you if any fields are blank. If any fields are blank then the form will not submit.

Also, 'onSubmit' isn't a valid form attribute, well it is a valid event. Get rid of it. The action attribute is where you need to place the path and script name of the PHP script to process the submit.

How exactly do you want the form to behave once the submit is clicked?

This post has been edited by CharlesEF: Jan 14 2019, 02:26 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 05:09 PM