WordPress.org

Ready to get started?Download WordPress

Forums

Passing arguments to PHP scripts within WordPress? (6 posts)

  1. johnstewart
    Member
    Posted 8 years ago #

    I've recently installed WP 2.0.2 on my site and am trying to integrate this little PHP script I did in the past.

    Basically, it lets you rate a picture (in this case, rather than "hotness", it's "grossness") and then it spits this into a mySQL table so you can keep track of the top gross and least gross pictures.

    The old PHP script is here:
    http://johnstewart.com/grossornot/

    And I tried adding the PHP code (with formatting junk stripped out) to a gross.php template and created a new page with gross.php as the template. Currently this is named "testgross" under my pages on the site:

    http://johnstewart.com/testgross/
    (yes, the formatting is all wonky still; I've not played with that yet)

    Anyway, the voting works by passing an argument op=rate through the form.

    Well, this argument doesn't survive the WordPress URL rewriting and all.

    For example, the script has a line saying:

    if ($op == 'rate')

    ...but the code in this block never gets executed (the PHP script never realizes op == rate).

    Is there any way to pass arguments to PHP code within templates such that the arguments survive whatever WordPress does to it?

    thanks!

  2. davidchait
    Member
    Posted 8 years ago #

    did you try ($_POST['op']=='rate) instead?

  3. johnstewart
    Member
    Posted 8 years ago #

    Yep, no difference. Here's the code (if it will let me post this)...

    thanks!

    johnS

    <?php
    /*
    Template Name: Gross
    */
    ?>

    <?php get_header(); ?>

    <P>
    <H3>Is this Gross or Not?</H3>

    <?
    include("gross_config.php");
    include("gross_prep.php");

    if ($_POST['op']=='rate')
    {
    $id = code2id($id);
    $record = $record = vote($id, $r);
    if ($record)
    {
    $oldid = $record["id"];
    $oldurl = $record["picurl"];
    $oldaverage = $record["voteaverage"];
    $oldaverage = sprintf("%1.1f", ($oldaverage / 10));
    $oldvotes = $record["votecount"];
    }
    $id = 0;
    } elseif ($_POST['op']=='bonk') {
    $id = code2id($id);
    bonk($id);
    $message = "The incident has been reported";
    }

    $id = code2id($id);
    if ($id == 0)
    while (($id==0) || ($id==$oldid))
    {
    $record = getRandomUser();
    $id = $record["id"];
    }
    else
    $record = getUser($id);

    $id = id2code($record["id"]);
    $active = $record["active"];
    if ($active != 0)
    $url = htmlspecialchars($record["picurl"]);
    else
    $url = "removed.gif";
    $thisurl = "http://johnstewart.com/grossornot/?id=$id";
    $bonkurl = "./?op=bonk&id=$id";
    $newurl = './user/?op=new';
    $geekboard = './g/?t=b';

    $rating = autoLogin();

    ?>

    <form action=./index.php name=_rateForm method=post><input type=hidden name=op value=rate><input type=hidden name=id value="<? print($id); ?>">
    Please select a rating to see the next picture.<BR>
    1<input type=radio name=r value=1 onclick="this.form.submit()">
    2<input type=radio name=r value=2 onclick="this.form.submit()">
    3<input type=radio name=r value=3 onclick="this.form.submit()">
    4<input type=radio name=r value=4 onclick="this.form.submit()">
    5<input type=radio name=r value=5 onclick="this.form.submit()">
    6<input type=radio name=r value=6 onclick="this.form.submit()">
    7<input type=radio name=r value=7 onclick="this.form.submit()">
    8<input type=radio name=r value=8 onclick="this.form.submit()">
    9<input type=radio name=r value=9 onclick="this.form.submit()">
    10<input type=radio name=r value=10 onclick="this.form.submit()">
    <P><P>
    <img src="<? print($url); ?>" border=3 vspace=4>

    <P>

    You can share this picture with a friend:<br>

    <? print($thisurl); ?><p>

    <?
    if (strlen($oldurl) == 0)
    {
    print stats();
    }
    ?>
    </font>
    <H3><A HREF="top10.php">Top Ten Grossest</A><BR><A HREF="bottom10.php">Top Ten Least Gross</A><BR><A HREF="http://johnstewart.com/blog/2006/04/02/gross-or-not/">Visit the Blog Entry</A></H3>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

  4. davidchait
    Member
    Posted 8 years ago #

    instead of:
    if ($_POST['op']=='rate')
    try something simple like:
    if (isset($_POST['op']) die($_POST['op']);

    That's a starting point.

    While $_POST does have stripslashes_deep and add_magic_quotes run on it (which might give you slightly different results than you expect...), I don't see anything that is otherwise doing something like unset($_POST[...]) or similar. The variables should be surviving.

    HOWEVER, given what you are trying to do, and the fact you are already using some minimal javascript, have you considered using some simple AJAX (well, AJAH, that is AJAX minus the xml...) instead of the standard HTML form posting? Then you could have a different piece of code, outside of WP's control, that processes the custom script... Just a thought.

  5. johnstewart
    Member
    Posted 8 years ago #

    Aha!

    Your suggestion did work (and thanks for the debug tip). $_POST['variable'] seems to work fine. The problem was I replaced instances of $op with $_POST['op'], but I hadn't done the same for $r and $id. After that, things seem to be working.

    Interesting suggestion RE: Ajax. I looked into it a bit; looks promising. However, I think I can do all I need to do here without it now that you provided the secret sauce.

    Thanks very much!

    johnS

  6. davidchait
    Member
    Posted 8 years ago #

    No problem, glad to help.

    -d

Topic Closed

This topic has been closed to new replies.

About this Topic