WordPress.org

Support

Support » WP-Advanced » php form on Page, in Page Template, not passing text into db

php form on Page, in Page Template, not passing text into db

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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.

    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

    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.

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘php form on Page, in Page Template, not passing text into db’ is closed to new replies.