Bad little Fuctions, running themselves...

Discussion in 'PHP' started by stauf, Jun 20, 2007.

  1. #1
    So hopefully this is the last hurdle for this, but it's got me stumped as it shouldn't be happening to begin with...

    Functions are made to be run on-call only, well mine are bad little children and run whenever the page is loaded. There's 4 functions, Read/Write Count, Write Preorder & Order. What I need is on page load, Read Count & Write Preorder run, and OnSubmit of the form, I need Write Count & Write Order run. Sounds simple, right..

    Any help greatly appreciated!

    
    import_request_variables('p', 'p_');
    
    // Counter Log Functions
    
    	// Read It - Current Count $ccount
    function readcounter(){
    global $ordercount;
    global $ccount;
    global $thecount;
    $frcount = fopen($ordercount, 'r') or die("uh-oh....");
    $thecount = fread($frcount, filesize($ordercount));
    $ccount = $thecount;   // used as echo variable in page for current count
    fclose($frcount);
    }
    
    	// Add It - Write It - Incremented $thecount
    function writecounter(){
    global $ordercount;
    global $thecount;
    $fwcount = fopen($ordercount, 'w') or die("uh-oh....");
    $thecount += 1;
    fwrite($fwcount, $thecount);
    fclose($fwcount);
    }
    
    
    // Setup Order Log
    function preorder(){
    global $preorderlog;
    global $cost;
    global $ship;
    global $model;
    global $engine;
    global $item;
    global $brake;
    global $ccount;
    global $p_nick;
    global $p_sscolor;
    global $p_camcolor;
    $fpre = fopen($preorderlog, 'a') or die("uh-oh....");
    fwrite($fpre, ("[".date("r")."] ".$p_nick." ".$model." ".$engine." | ".$item." | ".$cost." | ".$ship." | ".$p_sscolor." | ".$p_camcolor." | ".$brake." | ".$ccount."\n\n"));
    fclose($fpre);
    }
    
    // Make Order Log
    function makeorder(){
    global $orderlog;
    global $cost;
    global $ship;
    global $model;
    global $engine;
    global $item;
    global $brake;
    global $ccount;
    global $p_nick;
    global $p_sscolor;
    global $p_camcolor;
    $forder = fopen($orderlog, 'a') or die("uh-oh....");
    fwrite($forder, ("[".date("r")."] ".$p_nick." ".$model." ".$engine." | ".$item." | ".$cost." | ".$ship." | ".$p_sscolor." | ".$p_camcolor." | ".$brake." | ".$ccount."\n\n"));
    fclose($forder);
    }
    
    // Submit Functions
    function submitorder(){
    	writecounter();
    	makeorder();
    }
    
    // On-Load
    	readcounter();
    	preorder();
    
    PHP:
    
    <form action="" method="post" name="confirm" id="confirm" OnSubmit="<?php submitorder(); ?">
    
    HTML:
     
    stauf, Jun 20, 2007 IP
  2. stauf

    stauf Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Anyone? Please, I'll beg! :(
     
    stauf, Jun 20, 2007 IP
  3. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #3
    dude you need to put the onsubmit in the submit button html tag not the form tag and you need to put the onload attribute in the body html tag then this will work.

    for example <type input="submit" value="submit" type="submit" onclick="">
    sorry i said onsubmit, what i meant was onclick in the submit button.

    also you need the form to call a script not just "".!

    as for onload <body onload=""> </body> there
     
    uniqueasitis, Jun 20, 2007 IP
  4. stauf

    stauf Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks. I had tried the OnClick event handler on the Submit button and I don't get any different results. Everything still runs when the page loads, which is 'not' what I want. Also when I put the OnLoad handler on the body tag, the page itself refused to load/display/parse.
     
    stauf, Jun 20, 2007 IP
  5. Weizheng

    Weizheng Peon

    Messages:
    93
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You code looks funny - mixing php function with html.

    1. Try filling in the action field in the form with the php url/file.

    <form action="order.php?action=order" method="post">
    HTML:
    2. Move onload stuffs into a function:
    function onload() {
        readcounter();
        preorder();
    }
    PHP:
    3. In the php file, add this at the top:
    
    $action=$_REQUEST['action'];
    if ($action=='order') {
        order();
    } else if ($action=='bla bla') {{
        bla bla...
    } else {
       onload();
    }
    
    PHP:
     
    Weizheng, Jun 20, 2007 IP
  6. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The php functions are NEVER called when you click the buttons, but when the page loads, that's how php works: it parses the page when a request is sent, it will then execute the page. if you're mixing php and html, and you're trying to use php as javascript, it will never work.

    Here's an example:
    <?php
    function printHello(){
      echo "hello";
    }?>
    
    <html>
    <head>
    </head>
    <body>
    <input type="button" onclick="<?php printHello(); ?>" />
    </body>
    </html>
    PHP:
    will NOT print hello when you click the button, but instead will replace "<?php printHello(); ?>" in your html source code with "hello"

    What you need is javascript, probably AJAX to communicate between php and javascript. That's the only way you'll be able to use php functions just like javascript functions
     
    UnrealEd, Jun 21, 2007 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think that form of coding is how ASP.NET works, isn't it? But basically, everyone here is correct. On the page load you need to determine whether or not you have just been posted to and then run the code at that point.
     
    TwistMyArm, Jun 21, 2007 IP
  8. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #8
    why don't you just spread your php scripts function over several pages and then use javascript to call the appropriate page with the script. That is why i said you need an action in the form action field. If you want to keep the same page than you need to use ajax.
     
    uniqueasitis, Jun 21, 2007 IP
  9. stauf

    stauf Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks guys!

    What this lil project is, should be, simple. I have two pages, both with one form on them each. Page 1 (html) has the request form on it, which posts to Page 2 (php) which takes the post data, forms it how I want it, displays the info back to the user for verification, and has a Order button on it which submits the now php data into a hidden form for payment to paypal.
    The functions I want run are to log the initial info from page 1, so I have a record of every initial order. Then to log again when the person clicks to pay. I also want a counter to increment by 1 each time the order is paid for. So 3 log files total.

    It would seem tho that I need at least one more page, or I need to redo the php page to remove something.. unfortunately now my mind is at a loss...
     
    stauf, Jun 21, 2007 IP