WordPress.org

Forums

Custom Forms on Pages (4 posts)

  1. Quin
    Member
    Posted 3 years ago #

    Hello

    I am looking to add my own custom forms to various pages on my site.

    You're probably thinking 'just use a plugin', but to be honest, I don't like any of the plugins. I'm very pedantic and none of the plugins do what I want, or make it easy to do it.

    So I went to program my own form, and using Exec-PHP it loaded just fine.
    However, I am having some issues with the method. On the home page it loaded just fine where it was, but on /contact (for example) it does not work.

    Any ideas?

    <?php
    	// if "email" is filled out, send email
    	if (isset($_REQUEST['email'])) {
    		//send email
    		$name = $_REQUEST['name'] ;
    		$email = $_REQUEST['email'] ;
    		$phone = $_REQUEST['phone'] ;
    		$message = $_REQUEST['message'] ;
    		$subject = "Contact Mr Presenter" ;
    		mail("Quin452@gmail.com", "$subject",
    		$message, "From: " . $name);
    		mail($email, "$subject",
    		$message, "From: " . $name); ?>
    		<span style="font-size: 0.7em;">Your email has been sent, and a copy has been delivered to the email address provided.</span>
    	<?php }
    
    	//if "email" is not filled out, display the form
    	else { ?>
    		<form action="" method="post" onsubmit="return checkEmail(this)">
    			<label style="margin:0">Name:</label>
    			<input type="text" name="name" id="name" value="Please enter your name..." onfocus="if (this.value == 'Please enter your name...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please enter your name...';}" />
    
    			<label>Email:</label>
    			<input type="text" name="email" id="email" value="Please enter your email..." onfocus="if (this.value == 'Please enter your email...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please enter your email...';}" />
    
    			<label>Phone:</label>
    			<input type="text" name="phone" id="phone" value="Please enter your telephone number..." onfocus="if (this.value == 'Please enter your telephone number...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please enter your telephone number...';}" />
    
    			<label>How Can We Help?</label>
    			<textarea name="message" id="message" onfocus="if (this.value == 'Please enter your message') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Please enter your message';}" >Please enter your message</textarea>
    
    			<input type="image" name="Send" value="Send" src="<?php bloginfo('template_url'); ?>/images/send-button.png" />
    		</form>
    <?php } ?>
  2. peredur
    Member
    Posted 3 years ago #

    Does it just fail silently, or do you get an error message?

    Do the logs show anything? For example is the mail server doing what you expect with the message?

    Your code is not very safe, by the way. It's wide open to XSS attacks. You should never use the $_REQUEST array untested. As the PHP manual says:

    Note:

    The variables in $_REQUEST are provided to the script via the GET, POST, and COOKIE input mechanisms and therefore could be modified by the remote user and cannot be trusted.

    Note that client-side checking using JavaScript is not enough. It is trivial to circumvent client-side checks.

    Word to the wise and all that.

    Cheers

    PAE

  3. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    Also, the execPHP plugin is not the be all end all solution. It often fails. A plugin is often the way to go (even if it is one you make yourself) for theme independent items such as a form. But a page template would also work well.

    Some code just plain fails through that plugin

  4. Quin
    Member
    Posted 3 years ago #

    I know its not the best, but I just wanted to get it sorted out straight off (I've since got my favourite plugin to work).

    But with the program, it re-loaded the page and said that it couldn't be found. Exact same URL and everything.

    I got no emails... it just failed.

    Thanks for the advice :-)

Topic Closed

This topic has been closed to new replies.

About this Topic