使用带有参数/ AJAX的按钮onClick调用PHP函数?

使用带有参数/ AJAX的按钮onClick调用PHP函数?

问题描述:

Hello I want to call my function download when the user click on the button, basically:

<input type='button' name='Release' onclick="document.write('<?php downloadFichier($tab1, $t2) ?>');" value='Click to Release'>

Of course doesn't work, so I try to do with a AJAX, I don't know this language, but it is possible to do what I want: call my PHP function with 2 parameters?

<button type="button">Click Me</button>
<p></p>
<script type="text/javascript">
    $(document).ready(function(){
        $("button").click(function(){

            $.ajax({
                type: 'POST',
                url: 'file_product.php',
                success: function(data) {
                    $("p").text(data);
                }
            });
   });
});
</script>

Thank you for helping me.

您好我想在用户点击按钮时调用我的函数下载,基本上: p>

 &lt; input type ='button'name ='Release'onclick =“document.write('&lt;?php downloadFichier($ tab1,$ t2)?&gt;');”  value ='Click to Release'&gt; 
  code>  pre> 
 
 

当然不起作用,所以我尝试用AJAX做,我不懂这种语言, 但是可以做我想做的事情:用2个参数调用我的PHP函数? p>

 &lt; button type =“button”&gt;点击我&lt; / button&gt; 
&lt;  ; p&gt;&lt; / p&gt; 
&lt; script type =“text / javascript”&gt; 
 $(document).ready(function(){
 $(“button”)。click(function(){\  n 
 $ .ajax({
 type:'POST',
 url:'file_product.php',
 success:function(data){
 $(“p”)。text(data); \  n} 
}); 
}); 
}); 
&lt; / script&gt; 
  code>  pre> 
 
 

感谢您帮助我。 p> div>

You should call a js-function with onclick event like this:

<input type='button' name='Release' onclick="downloadFichier(param1, param2)" value='Click to Release'>

And your AJAX-function should be like this:

function downloadFichier(param1, param2){

        $.ajax({
            type: 'POST',
            url: 'file_product.php',
            data: "param1=" + param1 + "&param2=" + param2,
            success: function(data) {
                $("p").text(data);
            }
        });

You can get your params in PHP-script from the $_REQUEST array by their names (param1, param2).

@PaulBasenko inspired this alternative, where you set the parameters through some <input type="hidden" /> :

HTML

<form action="#" method="POST" id="form1">
    <input type="hidden" name="tab1" value="<?= $tab1 ?>" />
    <input type="hidden" name="t2" value="<?= $t2 ?>" />
    <button type="submit">Click to Release</button>
</form>

Javascript

$(function() { // equivalent of "$(document).ready(function(){"
    $('body').on('submit', '#form1', function(event) {
        event.preventDefault();

        var formData = $(this).serialize();

        $.ajax({
            type : 'POST',
            url : 'file_product.php',
            data : formData,
            success : function(data) {
                $('#p').text(data);
            }
        });
    });
});

PhP

<?php
    $tab1 = (isset($_POST['tab1'])) ? $_POST['tab1'] : null;
    $t2 = (isset($_POST['t2'])) ? $_POST['t2'] : null;

    // process & return json_encoded data
?>

How it works ?

When clicking on the button, which is a type="submit", it will trigger the submit event for its parent form. Then, jQuery listen to this event and immediately blocks it using èvent.preventDefault() in order to call Ajax instead of a regular synchronous call to a php file.