how to execute php code within javascript

前端 未结 8 1813
無奈伤痛
無奈伤痛 2020-11-30 03:47



        
8条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 04:08

    If you just want to echo a message from PHP in a certain place on the page when the user clicks the button, you could do something like this:

    
    

    However, assuming your script needs to do some server-side processing such as adding the item to a cart, you may like to check out jQuery's http://api.jquery.com/load/ - use jQuery to load the path to the php script which does the processing. In your example you could do:

    
    

    This runs the php script and loads whatever message it returns into

    .

    order_item.php would add the item to cart and just echo whatever message you would like displayed. To get the example working this will suffice as order_item.php:

    
    

    For this to work you will need to include jQuery on your page, by adding this in your tag:

    
    

提交回复
热议问题