6 Replies - 1185 Views - Last Post: 11 February 2013 - 08:38 AM

#1 shadytyrant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-March 12

posting a form with javascript, to work with php $_POST

Posted 07 February 2013 - 09:56 AM

Ok, so I have a form I am submitting with Javascript via an anchor tag. The form works fine when submitted with a input or button tag so I know everything works. The problem is I am checking with php to see if the form has been posted and when I submit with the anchor it does not work. I have already tried adding a hidden input with the name and value set to submit with no luck. Please help.

<h2>Contact <span>Form</span></h2>
<?php include("../content/includes/contact-email.php"); ?>
<form id="ContactForm" action="site/contact.php" method="post">
    <div>
    <div class="wrapper">
        <span><label for="name">Name:</label></span>
        <input type="text" class="input" id="name" name="name" />
    </div>
    <div class="wrapper">
        <span><label for="email">Email:</label></span>
        <input type="text" class="input" id="email" name="email" />                              
    </div>
    <div class="textarea_box">
        <span><label for="message">Message:</label></span>
        <textarea name="message" id="message" cols="1" rows="1" placeholder="Tell us about your experience with our Member businesses."></textarea>                               
    </div>
    <a href="site/contact.php" onclick="document.getElementById('ContactForm').submit();" class="button">    <strong>Send</strong></a>
    <a href="javascript:document.getElementById('ContactForm').reset();" class="button"><strong>Clear</strong></a>
    </div>
</form>                                                            



if(isset($_POST['submit'])){
 ... Send email ...
}



Is This A Good Question/Topic? 0
  • +

Replies To: posting a form with javascript, to work with php $_POST

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: posting a form with javascript, to work with php $_POST

Posted 07 February 2013 - 11:52 AM

View Postshadytyrant, on 07 February 2013 - 05:56 PM, said:

The problem is I am checking with php to see if the form has been posted and when I submit with the anchor it does not work. I have already tried adding a hidden input with the name and value set to submit with no luck. Please help.

and that’s exactly the reason why you should not test for the submit (button) to test if a form was submitted, but to check for the data itself you want to process.

btw. if you also leave the "contact.php" in the <a>’s href, you make a GET request instead of posting the form. easy to test with var_dump($_POST);. best you omit the href alltogether (in the other link as well).

This post has been edited by Dormilich: 07 February 2013 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 shadytyrant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-March 12

Re: posting a form with javascript, to work with php $_POST

Posted 07 February 2013 - 12:17 PM

Thank you so much you were exactly right! I have posted on several forums and no one could help. You got it right in one shot!

This post has been edited by Dormilich: 07 February 2013 - 12:45 PM
Reason for edit:: removed quote

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: posting a form with javascript, to work with php $_POST

Posted 07 February 2013 - 12:49 PM

well, thatís the advantage of the semantic web. if you know what each element is supposed to do, it is a comparatively easy task to find spots that are potentially problematic.

for instance, you could drop the <a> alltogether and define the click event on the <strong>. makes much more sense. the click event doesnít care, which element it is handled in, after all.
Was This Post Helpful? 1
  • +
  • -

#5 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: posting a form with javascript, to work with php $_POST

Posted 11 February 2013 - 08:30 AM

View PostDormilich, on 07 February 2013 - 12:49 PM, said:

for instance, you could drop the <a> alltogether and define the click event on the <strong>. makes much more sense. the click event doesnít care, which element it is handled in, after all.


Please forgive me if this is considered hijacking the topic, but this interested me.

Is it considered good practice to use the onclick event as a link? Would this have a negative effect on how your site is crawled and listed?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,045
  • Joined: 08-June 10

Re: posting a form with javascript, to work with php $_POST

Posted 11 February 2013 - 08:33 AM

View PostDarkranger85, on 11 February 2013 - 04:30 PM, said:

Is it considered good practice to use the onclick event as a link?

Iíd say yes. and itís inaccessible too.

though having a click event to submit a form is a different thing.


View PostDarkranger85, on 11 February 2013 - 04:30 PM, said:

Would this have a negative effect on how your site is crawled and listed?

I believe so. JS events are (afaik) not recognised by crawlers.
Was This Post Helpful? 0
  • +
  • -

#7 Darkranger85  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 31-August 12

Re: posting a form with javascript, to work with php $_POST

Posted 11 February 2013 - 08:38 AM

Thats too bad, that would make for some awesome sites lol.

Thanks for the info! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1