Jump to content

Recommended Posts

i'm working on a template and it has the following for submit button:

<a href="#" onclick="document.getElementById('contacts-form').submit()" class="button">send</a>

i'd like to know what is the 'getElementById' for and how do i detect that my form has been submitted? so that i can send the textfield values to the db.

Link to comment
https://forums.phpfreaks.com/topic/241591-detecting-form-submit/
Share on other sites

document.getElementById looks for and returns an object reference of an html element with the specified id attribute (in this case, 'contact-form').  I'm assuming that you have a form on your page and the opening form tag looks something like

 

<form name='something' id='contacts-form' method='post' action='somepage.php'>

 

 

So the document.getElementById('contacts-form') is supposed to be returning a reference to your contact form, and then the .submit() is supposed to submit it, and all this is supposed to happen when you click that link, since it is inside the onclick attribute.  So when you click on the link, that javascript code is executed and a request is made to whatever URL is in the action='...' attribute of your form tag. 

 

So you "detect" that the form has been submitted by virtue of the script running (whatever script is in the action attribute).  If the URL in the action attribute is the same as the page the form is on, or shares the same URL with other pages, then you will have to append a variable to the URL or pass a hidden field or similar, to let the script know that it was requested because of your form submission and not something else. 

document.getElementById looks for and returns an object reference of an html element with the specified id attribute (in this case, 'contact-form').  I'm assuming that you have a form on your page and the opening form tag looks something like

 

<form name='something' id='contacts-form' method='post' action='somepage.php'>

 

 

So the document.getElementById('contacts-form') is supposed to be returning a reference to your contact form, and then the .submit() is supposed to submit it, and all this is supposed to happen when you click that link, since it is inside the onclick attribute.  So when you click on the link, that javascript code is executed and a request is made to whatever URL is in the action='...' attribute of your form tag. 

 

So you "detect" that the form has been submitted by virtue of the script running (whatever script is in the action attribute).  If the URL in the action attribute is the same as the page the form is on, or shares the same URL with other pages, then you will have to append a variable to the URL or pass a hidden field or similar, to let the script know that it was requested because of your form submission and not something else.

Wow! thanx

but the 'action' attribute was empty

probably meaning that the processing to be taken in the same page

and i would also like to do that

passing a hidden field seems a good idea

but how do i do it?

yes, an empty action attribute means it posts to the same page.

 

Hidden field method would just be something like

 

<input type='hidden' name='contacts_form' value='true' />

 

Put that in your form and then server-side, look for that posted variable, same as any other posted variable.  But base a condition around it.

 

Dunno what language you are using but php example:

 

if ($_POST['contacts_form']) {
  // do database insertion stuff here
}

yes, an empty action attribute means it posts to the same page.

 

Hidden field method would just be something like

 

<input type='hidden' name='contacts_form' value='true' />

 

Put that in your form and then server-side, look for that posted variable, same as any other posted variable.  But base a condition around it.

 

Dunno what language you are using but php example:

 

if ($_POST['contacts_form']) {
  // do database insertion stuff here
}

thanx. worked well.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.