Support » Fixing WordPress » Custom Forms on Pages

  • 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?

    	// 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("", "$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...';}" />
    			<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...';}" />
    			<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" />
    <?php } ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • 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:


    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.



    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

    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 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Forms on Pages’ is closed to new replies.