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!

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.

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 !!!...

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!...

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.

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>';}

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>

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.

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.

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.

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?...

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!?

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.

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!?...

<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!?...

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?