0

I have a PayPal button I need to say "Sold Out" after clicking on it. When the button is click on the shopping cart, I need to run PHP code that adds "M1" to a file "trackOrder.txt". Then it checks the file for the string "M1". I will have other buttons will record to this file as well.

I need help combining this together so it functions. I currently initiating onmouseover for testing purposes.

<!--Calls Javascript test function-->
<form onmouseover="javascript:test()"; target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">        
    <input type="hidden" name="business" value="XXXXXXXXXXXXX">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="add" value="1">
    <input type="hidden" name="item_name" value="Men Accessory 1">
    <input type="hidden" name="amount" value="5.00">            
    <input type="hidden" name="currency_code" value="USD">
    Shipping Options:<br/>
    <select name="handling_cart">
        <option value="0.00">Free Local Pickup at our store</option>
        <option value="3.00">Standard Shipping - $3.00</option>
    </select>
    <br/>1 available</br>
    <input type="image" name="submit" src="http://charitysboutique.com/webpics/addtocart.gif" alt="Add to Cart">
    <img alt="" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif">
</form>

<?php
    function run(){
        $purchasedItem = "M1\n";
        $trackFile = fopen("trackOrder.txt", "a+");
        $txt = $purchasedItem;
        fwrite($trackFile, $txt);
        fclose($trackFile);
        echo "Finished";
    }
?>
<!--Calls PHP run function--> 
<script>
    function test() {   
        <?php run();?>
        alert("function test has been called")
    }
</script>

<!--open and read file-->
<?php
    $trackFile = fopen("trackOrder.txt", "r");
    if( strpos(file_get_contents("trackOrder.txt"),"M1") !== false) {
        echo "Found text";
    }
    fclose($trackFile);
?>
Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
  • Jquery, Javascript, Ajax, what have you tried? – Alex Jan 12 '18 at 01:03
  • I hit enter before I was finished with my explanation and no code. I have tried Ajax which I couldn't get working either. – Jonathan Blaine Jan 12 '18 at 01:06
  • 1
    @JonathanBlaine you can't trigger server side code with a js function this way. Typically you can bind something to the submit event and prevent the default action (actually submitting the form) using jquery, then use ajax to send a command to a php file that will do your file operations and report a json encoded response back and then using simple selectors you can change your text to print sold out or whatever. As I am unfamiliar with what you are trying to do (or why) I can't be of much help. – Alex Jan 12 '18 at 03:52
  • Thank you for the information. I will think about this some more when I have a little more time. Have some extended family issue we are trying to help out with. It is amazing how some thing so seemingly easy can be so darn complicated as just triggering an action. What I am trying to achieve is just to show that an item is no longer available to others when button is clicked. Just saving a simple string to file I can search for. If this string is found show image "out of stock" – Jonathan Blaine Jan 13 '18 at 03:33

0 Answers0