WordPress.org

Ready to get started?Download WordPress

Forums

php form on Page, in Page Template, not passing text into db (5 posts)

  1. mtheoryx
    Member
    Posted 7 years ago #

    Hi all, been searching for two days on how to do this. I am building a site using WordPress as the platform. I will need to have a variety of forms (registration forms for a non-profit camp). I am working on creating a very tiny test form first.

    I created a table in the wp database named wp_testTable. I only created one field called "name".

    Now I have this code in a page template called "testPage1.php" and a blank page using that page template :

    `<?php
    /*
    Template Name: TestPage1
    */
    ?>
    <?php # testPage1.php
    $page_title='Register';

    $fn=trim($_POST['name1']);

    $link = mysql_connect("localhost", "root", "root") or die("Could not connect: ". mysql_error());
    mysql_select_db("chttest") or die("could not select database");

    $query = "INSERT INTO wp_testTable (name) VALUES ('$fn')";

    $result = @mysql_query($query);

    ?>

    <?php get_header(); ?>

    <form action="<?php echo $PHP_SELF?>" method="post" name="name">
    <fieldset>
    <legend>Test Registration Form</legend>
    <p>
    <label for="name">Name</label>
    <input name="name1" id="name1" tyep="text" maxlength="20" value="<?php echo $_POST['name1']; ?>" />
    </p>
    </fieldset>

    <p>
    <input name="submit" class="submit" type="submit" value="Register" />
    </p>
    </form>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>`

    Here is my problem. When I click on the page in my sidebar, it comes up fine with a single field, and a submit button.

    I am able to type in text, press submit, and it goes immediately to the index page. That is fine, ill work on "success/fail results" page later.

    When I view the table in phpMyAdmin, however, there is no text. There is a new record, but no text in the record at all.

    Can anyone spot what I may have wrong here? I mean, it is working, it connects, and it writes to the db, but the actual text typed in is not passed.

    Appreciate any help!

  2. mtheoryx
    Member
    Posted 7 years ago #

    Oh, and the db is just a test bed on my local machine, so dont get all "hack-happy" about having the user/password, lol.

  3. Chris Burgess
    Member
    Posted 6 years ago #

    fyi - if you recycle the DB link wordpress already has handy (hint: global $wpdb + the database functions in the codex), you won't need to be packin the DB auth into pages separately

    hth

  4. RyuMaou
    Member
    Posted 6 years ago #

    Hey, I'm trying to do something similar with some php code that's based in a form, but the reference to $PHP_SELF seems to keep the form from passing the variables I'm setting.

    I've tried both ExecPHP and runPHP, but haven't gotten good results with either.

    Again, any ideas would be appreciated.

  5. RyuMaou
    Member
    Posted 6 years ago #

    Well, I did finally get my code to work in a test blog running WordPress v2.3 and runPHP. I had to change how the form populated some of the variables to do a proper logic check, but, after that, it worked.

    Using Moshu's method let me test and find out where things were going wrong.

Topic Closed

This topic has been closed to new replies.

About this Topic