Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

Wordpress form "process" page

So I am implementing a form that is on one site to another site.

One of the problems is that once the form is submitted an ajax function runs that sends the information to a "send-email" page. I have created a file in the themes directory named page-send-email.php which has all the necessary code but when the code runs I get a 404 error saying the script cant find www.siteURL/send-email. When I do this on the original site I am imitating everything gets posted and you can even visit that URL (although it is a blank page).

Any suggestions? I feel like this might have something to do with a config file?

8 Answers

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

Thanks for the advice everyone........ As in most situations when something seems like it should be obvious, it is. I forgot to publish the page from the wordpress admin, that's why I couldn't navigate to it.... (I had just saved the page-send-email.php page in my theme file).

Kevin Korte
Kevin Korte
28,149 Points

So to make sure I understand this, you have two sites: Site A and Site B?

Site B is wordpress? What is Site A?

Site A has a form, that form posts to Site B/send-email which has code to send an email. When you submit the form on Site A, via ajax, it says it can't find the URL.

Seeing all the code around this will help too.

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

nah, site A (which is a genesis wordpress site) has a form that posts to SiteA/send-email, site B (a regular wordpress site) has a form that posts to SiteB/send-email.

There is some simple code that tells the link to prevent default and fire this AJAX functionality:

function sendEmail () {

    $to = sanitize_email($_POST['to']);
    $from = sanitize_email($_POST['from']);
    $subject = sanitize_text_field(stripslashes($_POST['subject'])); //Name of the article
    $subject = "You May Like: " . $subject;
    $message = sanitize_text_field(stripslashes($_POST['body']));
    $bitly = $_POST['url'];
    $bitly = filter_var($bitly, FILTER_SANITIZE_URL);
    $body = '<html><body>';
    if ($message) {
        $body .= '<p>' . $message . '</p>';
    }
    $body .= $from . " has shared \"" . $subject . "\" with you - " . $bitly . "<br><br><br>";
    $body .= 'text text text text text text text text text text <a href="URLURLURL.com">here</a><br>';
    $body .= "For the latest updates follow us on <a href='URLURLURL.com'>Facebook</a> or <a href='URLURLURL.com'>Twitter</a>";
    $body .= "</body></html>";
    $headers = 'From: ' . $from . "\r\n" .
        'Reply-To: ' . $from . "\r\n" .
        'X-Mailer: PHP/' . phpversion() . "\r\n" . 
        "Content-Type: text/html; charset=ISO-8859-1\r\n";
    mail($to, $subject, $body, $headers);
}
Kevin Korte
Kevin Korte
28,149 Points

So both sites have the same problem? What's your ajax look like?

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

That was my mistake, that was just a function that fires in the page-send-email.php file. I don't think the ajax is the problem though because everything works on the site I am emulating. I think the problem is that wordpress can't find page-send-email.php on the new site (I can navigate to page-send-email.php on the original site, when I try to navigate to that page on the new site I get a page not found error).

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey David, Did you ever solve this problem?

David Jarrin
PLUS
David Jarrin
Courses Plus Student 11,182 Points

No, I'm still pretty confused as to why I can't navigate to the page send-email....its in my base themes directory