0

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?

Community
  • 1
  • 1
She Hulk
  • 39
  • 11
  • 1
    You can also `json_encode()` to pass information from php to back javascript. see this: http://api.jquery.com/jQuery.get/ – GBD Dec 09 '12 at 17:40

5 Answers5

4

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);
?>
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Instead of adding a script tag to the DOM why not use a proper xhr request and alert the response when needed in the success callback? – PeeHaa Dec 09 '12 at 17:40
  • That would be the preferred way to do it, yes. My answer is just the shortest path to make the current code work. – bfavaretto Dec 09 '12 at 17:42
  • The problem is: I never used Ajax before in my life, I guess I need to study a bit, before writing code. Anyway, thanks for the answers but I think there is some other error too, because it still doesn't work. – She Hulk Dec 09 '12 at 17:48
  • @SheHulk Added a more elegant solution. – bfavaretto Dec 09 '12 at 17:56
  • Make sure your HTML links to jQuery, and check your browser's console for errors (press F12). – bfavaretto Dec 09 '12 at 23:25
2

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.

Michoel
  • 834
  • 5
  • 16
0

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");
        }
});
Forrest Bice
  • 602
  • 5
  • 16
0

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.

RKINFOPHP
  • 21
  • 2
-1

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>
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198