1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with php in page

Discussion in 'XenForo Development Discussions' started by TRACKMIXERS, May 24, 2012.

  1. TRACKMIXERS

    TRACKMIXERS Member

    I am trying to use a php search script on a page.
    I can echo a simple "hello world" fine.

    I get this error though when trying my script, any help is appreciated.

    Server Error

    Undefined index: kw
    1. XenForo_Application::handlePhpError() in XenForo/Pages/test.php at line 2
    2. require() in XenForo/Pages/plugin.php at line 7
    3. XenForo_Pages_plugin::includePhpFile()
    4. call_user_func_array() in XenForo/ControllerPublic/Page.php at line 46
    5. XenForo_ControllerPublic_Page->actionIndex() in XenForo/FrontController.php at line 310
    6. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
    7. XenForo_FrontController->run() in /home/xxxxxr/public_html/testxf/index.php at line 13
    Here is my code in test.php:
    PHP:
    <?php
    $search 
    $_POST["kw"];
    if (
    $search == ''// If value does not equal anything
    {
    $search "media";
    } else { 
    // if value is not empty
    ;
    }
    ?>
    My plugin.php file for callback looks like this:
    PHP:
    <?php
    class XenForo_Pages_plugin
    {
        public static function 
    includePhpFile(XenForo_ControllerPublic_Abstract  $controllerXenForo_ControllerResponse_Abstract &$response)
        {
            
    ob_start();
            require(
    'library/XenForo/Pages/test.php');
            
    $myContent ob_get_contents();
            
    ob_end_clean();
     
            
    $params = array(
                
    'myContent'  => $myContent
            
    );
     
            
    $response->params array_merge(
                
    $response->params,
                
    $params
            
    );
        }
    }
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Replace:

    Code:
    $search = $_POST["kw"];
    
    With:

    Code:
    $search = (!empty($_POST["kw"]) ? $_POST["kw"] : '');
    
     
  3. TRACKMIXERS

    TRACKMIXERS Member

    Thanks Jake!
     
  4. TRACKMIXERS

    TRACKMIXERS Member

    I had one more question I wanted to ask...

    I want to replace "media" in this code so it would show the page title of whatever page the user is on. How could I go about this?
    Code:
    <?php
    $search = $_POST["kw"];
    if ($search == '') // If value does not equal anything
    {
    $search = "media";
    } else { // if value is not empty
    ;
    }
    ?>
     
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That may be available in $response->params. Do a dump in your callback function to see what's in there:

    Code:
    exit(var_dump($response->params));
    
    $page['title'] should be available in there.
     
    Yorick likes this.
  6. TRACKMIXERS

    TRACKMIXERS Member

    Thanks again, Jake. I finally got it with this:
    PHP:
    <?php
    $search 
    = (!empty($_POST["kw"]) ? $_POST["kw"] : '');
    if (
    $search == ''// If value does not equal anything
    {
    $search $response->params['page']['title'];
    } else { 
    // if value is not empty
    ;
    }
    ?>
    Thanks, kicken :)
     
  7. TRACKMIXERS

    TRACKMIXERS Member

    I have everything working the way I want but I have one more problem I cannot figure out. I keep getting this error: Security error occurred. Please press back, refresh the page, and try again.
    It happens when I select an option from the form below. The form is on a form.php page that is called by my included test.php page. Any help is greatly appreciated.

    Code:
    <script type="text/javascript">
    <!-- Begin
    function Sort_%%ID%%(){
      document.forms['sort_%%ID%%'].submit();
    }
    // End -->
    </script>
    <form name="sort_%%ID%%" id="sort_%%ID%%" method="post" action="" style="width:100%;margin:0; text-align:right;">
      <select name="sortnum_%%ID%%" onchange="Sort_%%ID%%();" style="width: 225px;">
        <option value="BestMatch">Best Match</option>
        <option value="EndTimeSoonest">Items Ending First</option>
        <option value="StartTimeNewest">Newly-Listed Items First</option>
        <option value="PricePlusShippingLowest">Price + Shipping: Lowest First</option>
        <option value="PricePlusShippingHighest">Price + Shipping: Highest First</option>
      </select>
    <input name="country" type="hidden" value="%%country%%" />
    </form>
    It found this thread and it looks like I need to add this to my form but I am stuck on how to accomplish this.

    Code:
    <input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />
     
  8. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Code:
    <script type="text/javascript">
    <!-- Begin
    function Sort_%%ID%%(){
      document.forms['sort_%%ID%%'].submit();
    }
    // End -->
    </script>
    <form name="sort_%%ID%%" id="sort_%%ID%%" method="post" action="" style="width:100%;margin:0; text-align:right;">
      <select name="sortnum_%%ID%%" onchange="Sort_%%ID%%();" style="width: 225px;">
        <option value="BestMatch">Best Match</option>
        <option value="EndTimeSoonest">Items Ending First</option>
        <option value="StartTimeNewest">Newly-Listed Items First</option>
        <option value="PricePlusShippingLowest">Price + Shipping: Lowest First</option>
        <option value="PricePlusShippingHighest">Price + Shipping: Highest First</option>
      </select>
    <input name="country" type="hidden" value="%%country%%" />
    <input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />
    </form>
    
     
  9. TRACKMIXERS

    TRACKMIXERS Member

    Thanks Jake, I've tried that already but it doesn't seem to work.
     

Share This Page