简体   繁体   中英

Run php code when clicking on link

I need to execute some php code when a user clicks on a link. I found an answer here and I wrote this in the site header:

<script type="text/javascript">
function sottoponi() {
$.get("sottoponi.php");
return false;
}
</script>

this in the body, where I need the code:

<a href="#" onclick="sottoponi();">Prova</a>

And then I wrote this on sottoponi.php

<?php 
$variable= value; 

if ($variable < 1) {
// do some php code;
}

else if ($variable > 1) {
$jav1 = <<<MAR1
<script language="javascript" type="text/javascript">
alert("Error!");
</script>
MAR1;
echo $jav1;
}

?>

I'm sure I'm doing it wrong, because I don't get the alert (and I'm sure $variable > 1 is true, I echoed the variable!): how can I pass information from sottoponi.php to the actual page?

You don't get the alert because your generated js code is not being added to the page. To run it, you need to take the response from the ajax request and add it to the DOM:

function sottoponi() {
    $.get("sottoponi.php", function(response){
        $('body').append(response);
    });
    return false;
}

Note that this solution is not very elegant. What I would do in this kind of situation is respond with JSON from my PHP, so we can easily know from ths js side if the operation succeeded of not:

function sottoponi() {
    $.getJSON("sottoponi.php", function(response){
        if(response.error !== false) {
            alert("ERROR!");
        }
    });
    return false;
}

The PHP for that would be:

<?php 
$variable = value; 
$response = array('error' => false);

if ($variable < 1) {
    // some php logic;
} else if ($variable > 1) {
    $response['error'] = true;
} else {
   // some more logic here if needed
}
echo $json_encode($response);
?>

If you want to send a whole script and have it processed you should use jQuery's getScript function instead: $.getScript("sottoponi.php"); although it would be less confusing to just pass a callback function to get() to handle the result:

$.get("sottoponi.php", function(res) {
    alert(res);
});

which, if it is a complicated structure, you can encode with PHP's json_encode .

You are using jQuery.get() which is shorthand for jQuery.ajax() . Thus, the following is a possibly more clear way you could go about submitting to sottoponi.php:

$.ajax({
    url: "sottoponi.php",
        type: "POST",
        success:function(result){
            //You can place an alert here such as: alert("Successful Ajax Request Complete");
        }
});

Heh heh, we clarify the matter :-). Well, I tell you. The order of the process is the opposite to that comment. would record first data in the table and then proceed to the download. Leveraging the code I gave you, we would have: if (empty ($ _GET ['filename']) { $ query = "INSERT INTO LOWERED VALUES ('". $ program. "', '". $ username. "' '". $ time."') " if ($ result = mysql_query ($ query, $ link)) { / / recorded correctly } else { / / No record was recorded } / / proceed to the download. header ( "location:". $ program); } else { echo "Error. Invalid file"; } Hope this helps (I have not delved too much into the code, since there is no connection to the database server, the selection of base, etc ...), but basically would. course, you could modify it and put the header inside the if, when recorded properly record in the table, to have more control over the download. Greetings.

You mix PHP and Javascript. You cannot call alert() in PHP code. If you want an alert() you must do so in Javascript

<script type="text/javascript">
function sottoponi() {
$.get("sottoponi.php");
alert("Error!");
return false;
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM