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

PHP in Page Node

Discussion in 'XenForo Questions and Support' started by Zovator, Dec 30, 2012.

  1. Zovator

    Zovator Member

    Hello,
    I have a simple PHP file with a form that connects to a MySQL database and sends application information to the database. I'm not sure how to include this PHP in a page node, does anyone know how I would do this? If you have an alternative way for doing this I would like to hear it as well.
    ~ Thanks.
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Chris D likes this.
  3. Zovator

    Zovator Member

    That does help, thanks. How would I incorporate a form into this?
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The form HTML would go into your page template when editing the page node in your Admin CP.
     
    Chris D likes this.
  5. Zovator

    Zovator Member

    Hmm, what would I put in the
    Code:
    action=""
    part of the form?
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The correct way would be to use xen:link syntax:

    Code:
    action="{xen:link 'pages', $page}"
    
    That will parse to the URL of the page, so the form will submit to itself. Then you can check for the form submission in your callback.
     
  7. Zovator

    Zovator Member

    Ok, thanks. I'm not sure why, but I keep getting:
    when I submit with the form.
     
  8. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Include this hidden field in your form:

    Code:
    <input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />
    
    That will satisfy the CSRF check which should fix that error.
     
  9. Zovator

    Zovator Member

    Seems to have worked, thanks, one more problem, how do I get a field from the form into this?:
     
  10. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Use the $_REQUEST array:

    Code:
    $_REQUEST['fieldname']
    
     
  11. Zovator

    Zovator Member

    I'm getting 'undefined index', I have it like this:
    and
    Sorry for the hassle..
     
  12. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    You should only access that variable on a form submission.

    Code:
    $request = $controller->getRequest();
    
    if ($request->isPost())
    {
    	// THEN PROCESS FORM SUBMISSION
    }
    
    ...assuming your HTML form has method="POST"
     
  13. Zovator

    Zovator Member

    Oh! Right, thank you very much for your help :)
     
  14. Zovator

    Zovator Member

    Sorry to bother you again, I added all the forms and now I'm getting:
    What does this mean?
     
  15. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That is a problem with a db call. I need to see the code that generates that error.
     
  16. Zovator

    Zovator Member

    Code:
    <?php
     
    class Callback_PageNode
    {
        public static function candidacyapply(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
        {
            $mydb = new Zend_Db_Adapter_Pdo_Mysql(array(
                'host'    => 'localhost',
                'username' => 'members',
                'password' => '---------',
                'dbname'  => 'members'
            ));
           
            $request = $controller->getRequest();
     
    if ($request->isPost())
    {
                $mydb->insert(
                'pending',
                array('Username' => $_REQUEST['formusername'], 'Timezone' => $_REQUEST['formtimezone'], 'Given Name' => $_REQUEST['formname'], 'Last Name Initial(s)' => $_REQUEST['forminitials'], 'Ship 1' => $_REQUEST['formship1'], 'Ship 2' => $_REQUEST['formship2'], 'Ship 3' => $_REQUEST['formship3'], 'Ship 4' => $_REQUEST['formship4'], 'Ship 5' => $_REQUEST['formship5'], 'Ship 6' => $_REQUEST['formship6'], 'Ship 7' => $_REQUEST['formship7'], 'Ship 8' => $_REQUEST['formship8'], 'Ship 9' => $_REQUEST['formship9'], 'Ship 10' => $_REQUEST['formship10'], 'Ship 11' => $_REQUEST['formship11'], 'Ship 12' => $_REQUEST['formship12'], 'Ship 13' => $_REQUEST['formship13'], 'Ship 14' => $_REQUEST['formship14'], 'Ship 15' => $_REQUEST['formship14'], 'Ship 1 Given Name' => $_REQUEST['formshipname1'], 'Ship 2 Given Name' => $_REQUEST['formshipname2'], 'Ship 3 Given Name' => $_REQUEST['formshipname3'], 'Ship 4 Given Name' => $_REQUEST['formshipname4'], 'Ship 5 Given Name' => $_REQUEST['formshipname5'], 'Ship 6 Given Name' => $_REQUEST['formshipname6'], 'Ship 7 Given Name' => $_REQUEST['formshipname7'], 'Ship 8 Given Name' => $_REQUEST['formshipname8'], 'Ship 9 Given Name' => $_REQUEST['formshipname9'], 'Ship 10 Given Name' => $_REQUEST['formshipname10'], 'Ship 11 Given Name' => $_REQUEST['formshipname11'], 'Ship 12 Given Name' => $_REQUEST['formshipname12'], 'Ship 13 Given Name' => $_REQUEST['formshipname13'], 'Ship 14 Given Name' => $_REQUEST['formshipname14'], 'Ship 15 Given Name' => $_REQUEST['formshipname15'], 'Rank' => $_REQUEST['rank'], 'Why did you choose the SUN?' => $_REQUEST['formwhydidyouchoose'], 'What can you contribute?' => $_REQUEST['formwhatcanyougive'], 'How did you find out about SUN?' => $_REQUEST['formhowdidyoufind'], 'Do you have any specialties?' => $_REQUEST['formspecialties'], 'How do you see yourself playing SC?' => $_REQUEST['formplaying'], 'Extra comments/queries' => $_REQUEST['formcomments'])
                );
               
    }
        }
    }
     
  17. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    The insert specifies the column names are their values. But you appear to be using invalid column names (containing spaces). Look at the table in the database to see the column names.
     
  18. Zovator

    Zovator Member

    Capture.PNG
    Weird, they have spaces in the table.
     
  19. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Hmm. I assumed column names weren't allowed to have spaces. I guess I never tried it in 10 years of working with MySQL databases. =\

    The ? character is normally the token. Some of your column names have a ? in them. Is that correct? That might be causing the error. Maybe the token can be escaped. I don't know if this is possible or necessary. I would just remove the ? character from your column names. That may fix the error.
     
  20. Zovator

    Zovator Member

    Ah! That did it, thank you very much!
     

Share This Page