简体   繁体   中英

Wordpress custom form action

I've searched two day's all over the web and forums to find the solution for my problem (this site as well). Some questions here were similar but didn't help me at all. Ok to get to the point, I'd be very thankful if someone could asist me.

I've made a guestbook in my custom wordpress theme. This guestbook shows up just the way I wanted. Everything is set in phpMyAdmin and works. Only problem i'm having is the form action="" doesn't work. When I leave it empty and I fill in the guestbook form, it just shows a blank content.

What can I fill in between these quotes action="" Answers on sites and forums say leave it empty, but it doesn't give me the results I want. When I fill in the guestbook form it should show the message when someone submits.

Here is my code, perhaps i'm doing something wrong?

    <?php 

    mysql_connect("blablabla", "blablabla", "blablabla");
    mysql_select_db("blablabla");

    /***************************************************************************************************************/
    //  Form and add stuff area

    echo "<strong>Laat anderen uw mening weten over Bed & Breakfast Nerja</strong>";

    if ($_POST['bericht_btn']) {
            $naam = strip_tags($_POST['naam']);
            $email = strip_tags($_POST['email']);
            $bericht = strip_tags($_POST['bericht']);

            if ($naam && $email && $bericht) {

                    $tijd = datum("h:i A");
                $datum = datum("F d, Y");
                $ip = $_SERVER['REMOTE_ADDR'];

                // Voeg toe aan database
                mysql_query("INSERT INTO gastenboek VALUES (
                        '', '$naam', '$email', 'bericht', 'tijd', 'datum', 'ip'
                )");

                echo "Bedankt. Uw bericht is succesvol toegevoegd aan ons gastenboek.";
            }
            else
                echo "Niet alle velden waren ingevuld.";
            }

            echo "<form action='' method='post' id='form_guestbook'>
                <table><br />
                        <tr>
                            <td>Naam:</td>
                            <td><input type='text' name='naam' style='width: 200px;' /></td>
                        </tr>
                        <tr>
                            <td>Email:</td>
                            <td><input type='text' name='email' style='width: 200px;' /></td>
                        </tr>
                        <tr>
                            <td>Bericht:</td>
                            <td><textarea name='bericht' style='width: 450px; height: 100px; margin-bottom: 20px;'></textarea></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type='submit' name='bericht_btn' value='Plaats bericht' /></td>
                        </tr>
                </table>
            </form>
            <br />";

    /***************************************************************************************************************/
    //  Display stuff area

    //echo "Alle gedeelde meningen";
        echo "<div style='color: red; font-size: 20px;'>Het gastenboek werkt nog niet compleet. Hier wordt aan gewerkt.</div>";                       
    $query = mysql_query("SELECT * FROM gastenboek ORDER BY id DESC");
    $numrows = mysql_num_rows($query);

    echo "<hr />";

    if($numrows > 0){
            while ( $row = mysql_fetch_assoc($query) ) {
                    $id = $row['id'];
                $naam = $row['naam'];
                $email = $row['email'];
                $bericht = $row['bericht'];
                $tijd = $row['tijd'];
                $datum = $row['datum'];
                $ip = $row['ip'];

                $bericht = nl2br($bericht);

                echo "<div>
                        Bericht van <strong>$naam</strong> op <strong>$datum</strong> om <strong>$tijd</strong> <br />
                        $bericht
                        </div><hr />";
                }
            }
            else
                    echo "Er zijn geen berichten gevonden.";

    /***************************************************************************************************************/

    mysql_close();

?>

This code is in the gastenboek.php (means guestbook.php in dutch) and it's included in my page.php and is shown only for a certain page (the guestbook page).

    <?php
      if ( is_page('gastenboek') ) {
    include 'gastenboek.php';
  }
    ?>

The end page url is as followed mywebsite.nl/wordpress/gastenboek (seo friendly url)

Thanks if someone could assist/help me, I think the problem isn't that big, but I just can't seem to find the solution thanks to my lack of knowledge with custom forms that are compatible with wordpress.


Ok i'm getting closer in solving the problem. I already tried the guestbook without wordpress by just using a guestbook.php file on my server. Here I was able to use

    <form action="../guestbook.php"

Guess what, it worked xD ! So the problem is one of the following things or probably both of them:

  • Where should I place my code in the wordpress .php files ? (currently it's in the page.php file where I have a if statement to include the guestbook.php only for a certain page)

     if ( is_page('name of a page') ) { include 'guestbook.php'; }
  • What must I type between the quotes of action="" ? (I've tried many different things, path to guestbook.php, keeping it empty, using the permalink code and none worked so far for me)

Change:

if ($_POST['bericht_btn']) {

to:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

clicking a submit does not post the name attribute of the submit.

Alternatively you can use a hidden input. To do so , you would put this in your form:

<input type="hidden" name="submitted" />

And then use this check in your form handler:

if (isset($_POST['submitted']))) {

As for the action attribute of the form, it should be set to the url of the script which handles it. It looks like the script which displays the form is the same script which handles the form, so if this script is called "foo.php" you would set the action attribute to "/foo.php".

Problem solved !!!

I had to do the following things: add ID to the form and change the names of the input fields since it was conflicting with wordpress.

I changed the names of the input types before, but it didn't work than. I simply forgot to change it for this code as well:

    $name = strip_tags($_POST['formname']);
                            $email = strip_tags($_POST['formemail']);
                            $message = strip_tags($_POST['formmessage']);

Anyway thanks for your reply Neil Girardi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM