Skip to content Skip to sidebar Skip to footer

Javascript Submit Is Not Sending Post To Php File

I am having trouble getting my javascript hyperlink button to post data to my php form email script. I have tried doing it several different ways, but nothing seems to work. Any he

Solution 1:

Remove enctype="text/plain" from the form tag, PHP doesn't support it.

See: method="post" enctype="text/plain" are not compatible?

Solution 2:

While I know this does not answer your question there is no reason to use Javascript for this purpose (in the example above) as the HTML input elements will handle both of these functions automatically.

<inputtype="reset" value="Clear" />
<inputtype="submit" value="Send" />

I would only work on doing something like this for validating data and then I'd use the onsubmit action of the form (returning false if validation fails).

Solution 3:

Just replace a tag as below.

<a href="Javascript:void(0);"

and also replace document.forms["myForm"].submit();

and add name="myForm" to your form.

Complete Code

<formid="form"action="contactform.php"method="POST"name="myForm"><fieldset><label><strong>Your Name:</strong><inputid="name"type="text"name="name"></label><label><strong>Your Phone Number:</strong><inputid="phone"type="text"name="phone"></label><label><strong>Your E-mail:</strong><inputid="email"type="text"name="email"></label><label><strong>Your Message:</strong><textareaid="message"name="message"></textarea></label><divclass="btns"><ahref="contacts.html"class="button">Clear</a><ahref="Javascript:void(0)"onclick="document.forms['myForm'].submit();"class="button">Send</a></div></fieldset></form>

Solution 4:

This will work:

<ahref="#"onclick="document.form.submit();"class="button">Send</a>

You don't need to getElementById, you can simply reference it this way.

Solution 5:

I tested this and it works....

<html><body><formid="form"action="contactform.php"method="POST"><fieldset><label><strong>Your Name:</strong><inputname="name"type="text"name="name"></label><label><strong>Your Phone Number:</strong><inputname="phone"type="text"name="phone"></label><label><strong>Your E-mail:</strong><inputname="email"type="text"name="email"></label><label><strong>Your Message:</strong><textareaname="message"name="message"></textarea></label><divclass="btns"><ahref="contacts.html"class="button">Clear</a><ahref="#"onclick="document.forms[0].submit();"class="button">Send</a></div></fieldset></form></body></html><?php

var_dump($_REQUEST);

echo"<BR>";

foreach ( $_POSTas$key => $value )
{
    echo$key . " " . "=" . " " . $value;
    echo"<BR>";
}

?>

Some other tips for you to consider are:

  • don't use <strong>. instead use css classes as you did with the hyperlink. example label { font-weight:bold } or <label class='strong'>
  • consider using jquery and unobtrusive javascript. example $("form").first().on("submit",function...
  • don't apply the label over the input. simply close the label tag and then use the input, or use <label for="input1"> to match to an input and enable a checkbox when the text is clicked for example.
  • specify a name attribute as that is what is sent to the server on post...the id is for script referencing in the browser

Post a Comment for "Javascript Submit Is Not Sending Post To Php File"