Calling one PHP script from another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebarry
    New Member
    • Jul 2007
    • 69

    Calling one PHP script from another

    Hi,

    I'm using the include statement to execute one PHP script from another, but I'm using smarty to separate HTML from PHP. The HTML is executed and the form posts back to the PHP file. The PHP code completes but does not return to the PHP script from which it was included.

    What am I doing wrong?

    Thanks,

    Sean
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi Sean.

    The include() function inserts the PHP code from the file you specify into the current code. So if the file you include has has an exit() or header() function which causes the code to stop executing or causes the page to be redirected, the rest of the original code, after the include() function will never get executed.

    Could you post the code that is causing the problem? It may help us understand exactly what is going on.

    Comment

    • Sebarry
      New Member
      • Jul 2007
      • 69

      #3
      Hi,

      Yes of course. I didn't before because it would be overkill to include all my code. Here is the code relating to what I want to code.

      UserSubmitJob.p hp

      [CODE=php]
      include( 'UserJobContact Info.php' );
      var_dump( $_POST );
      [/CODE]

      UserJobContactI nfo.php
      [CODE=php]
      if( !isset( $_POST['submitContactI nfo'] ) )
      {
      $selfChanged = "UserJobContact Info.php";
      $smarty->assign( 'selfChanged', $selfChanged );
      $smarty->display('job s/UserJobContactI nfo.tpl');
      }
      else
      {
      // Do lots of stuff here
      echo "Here";
      }
      [/CODE]

      The include works and
      Code:
      UserJobContactInfo.php
      is executed. This script uses smarty to display a HTML form which when completed posts data back to
      Code:
      UserJobContactInfo.php
      . The comment do lots of stuff here replaces the processing on that data, which is not important here. The code completes and the echo is printed but execution doesn't return to
      Code:
      UserSubmitJob.php
      and the var_dump is never executed. Do I need an exit statement?

      Hope this makes sense.

      Thanks,

      Sean
      Last edited by Atli; Oct 10 '07, 02:44 PM. Reason: Changed [code] tags to [code=php] tags.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ok, I see.

        I think the problem is that you are posting the form data to 'UserJobContact Info.php' rather than 'UserSubmitJob. php'.

        When the form is submitted, it redirects the browser, calling 'UserJobContact Info.php', bypassing the include() in 'UserSubmitJob. php' and going straight to 'UserJobContact Info.php'. So the var_dump() function is called when the form is printed, there is simply nothing to print.

        Try changing the form, having it submit it's data to 'UserSubmitJob. php' instead of 'UserJobContact Info.php'.

        Comment

        • Sebarry
          New Member
          • Jul 2007
          • 69

          #5
          Hi,

          Yes I could change the form action in the smarty template for 'UserJobContact Info.php' to 'UserSubmitJob. php' but that would mean copying the form processing data from UserJobContactI nfo.php which is a script that I want to reuse.

          By not changing the HTML form it posts to UserSubmitJob.p hp. By changing it posts to UserJobContactI nfo.php which is what I want but UserJobContactI nfo.php appears to complete. Printing the string here but doesn't go back to UserSubmitJob.p hp from which it was included.

          Sean

          Originally posted by Atli
          Ok, I see.

          I think the problem is that you are posting the form data to 'UserJobContact Info.php' rather than 'UserSubmitJob. php'.

          When the form is submitted, it redirects the browser, calling 'UserJobContact Info.php', bypassing the include() in 'UserSubmitJob. php' and going straight to 'UserJobContact Info.php'. So the var_dump() function is called when the form is printed, there is simply nothing to print.

          Try changing the form, having it submit it's data to 'UserSubmitJob. php' instead of 'UserJobContact Info.php'.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by Sebarry
            Hi,

            Yes I could change the form action in the smarty template for 'UserJobContact Info.php' to 'UserSubmitJob. php' but that would mean copying the form processing data from UserJobContactI nfo.php which is a script that I want to reuse.

            By not changing the HTML form it posts to UserSubmitJob.p hp. By changing it posts to UserJobContactI nfo.php which is what I want but UserJobContactI nfo.php appears to complete. Printing the string here but doesn't go back to UserSubmitJob.p hp from which it was included.

            Sean
            OK. This is getting a little confusing...

            When you submit a form, your browser is redirected, creating an entirely new Request. Your include, which was created in the previous request, is thrown away, forgotten.

            You can submit your form to the 'UserSubmitJob. php' without changing a thing in your code. It will include the 'UserJobContact Info.php' and run the form processing code that you put there. (This is all based on the code you posted, of course)

            The include() function imports the code of the file you specify into the current code and executes it.

            And by the time you see anything in your browser, all the PHP code has been executed. What happens after you post your form has nothing to do with the code executed before it was shown.

            Comment

            Working...