Jump to content

validation by 'required' before storing of page data by php file into database


Falko

Recommended Posts

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 fulfill the 2 conditions above:
* <FORM NAME="Betrugseingabe" ACTION="../cgi-bin/DBinsert.php" METHOD=POST>
* <INPUT TYPE="submit" NAME="Absenden" VALUE="Absenden" ID="Schaltflaeche1" formtarget="_self">

Link to comment
Share on other sites

First! Always check your inputs server-side! Clients can send you whatever the heck they want! The required attributes are good enough for people who wish to use your website legitimately, but you have to be careful of those who don't.

To get what you desire, you'll want to create an AJAX request.

Its not my usual tactic to hand out code, but if you want it
 

HTML:

<FORM NAME="Betrugseingabe">
<INPUT TYPE="submit" NAME="Absenden" VALUE="Absenden" ID="Schaltflaeche1" ONCLICK="send_form();return false;">

JavaScript:

function handle_response() {
  if(this.status == 200){
    //Output Success or something?
    //Redirect?
    //Something happens if you don't error out
  }
}

function send_form() {
  var the_form = document.querySelector("form");
  var formData = new FormData(the_form);
  var request = new XMLHttpRequest();
  request.addEventListener("load", handle_response)
  request.open("POST", "../cgi-bin/DBinsert.php");
  request.send(formData);
}

Disclaimer: Untested

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...