-6

I searched up some questions on stackoverflow and used the suggested solutions, however, I can't seem to get my php code working the way I'd like.

When I visit projects.php?message=success or projects.php?message=removed, the JavaScript function does not execute. In my debugging, I've confirmed the JavaScript is working correctly by attaching it to a button with the onclick property.

My php code:

<?php 
function GET($key) {
    return isset($_GET[$key]) ? $_GET[$key] : null;
}

$alert= GET('message');
if ($alert == success) {
    echo '<script type="text/javascript"> window.onload=success(); </script>';
} elseif ($alert == removed) {
    echo '<script type="text/javascript"> window.onload=removed(); </script>';
}

?>

My JavaScript code:

    <script>
    function success() {
        $.notify({
            // options
            icon: "pe-7s-cloud-upload",
            message: 'New project entry was successfully added.' 
        },{
            // settings
            type: 'success'
        });
    }
    function removed() {
        $.notify({
            // options
            icon: "pe-7s-trash",
            message: 'Project entry was successfully deleted.' 
        },{
            // settings
            type: 'danger'
        });
    }
</script>
Ajility
  • 526
  • 3
  • 19
  • 1
    What is your exact question? – Jay Blanchard Jan 05 '17 at 15:31
  • 3
    `if ($alert == "success")` Note the quotes. –  Jan 05 '17 at 15:33
  • what does a button have to do with anything. 'post your code' doesn't mean post any arbitrary code, it means post the relevant code. if the problem has to do with a button why aren't we seeing any button code? – I wrestled a bear once. Jan 05 '17 at 15:35
  • @Iwrestledabearonce. He says he used a button to confirm the JS code works. Both are unrelated to what's causing the problem though. –  Jan 05 '17 at 15:37
  • @Iwrestledabearonce. I've included all relevant code. The post says I used a button to call the JavaScript through onclick and confirm its working. I apologize but the way I think may be different- I used this as debugging to rule out the issue and confirm it was in the PHP and not my JavaScript. – Ajility Jan 05 '17 at 15:40
  • @ChrisG You are right, that was a subtly irrelevant debugging step I took that I suppose the community here didn't take well. – Ajility Jan 05 '17 at 15:41
  • @SamiAji - sorry it's probably too early for me to be on SO. did Chris' suggestion help? – I wrestled a bear once. Jan 05 '17 at 15:42
  • 2
    @SamiAji The community didn't take well that you missed an obvious error by not testing your PHP code properly. –  Jan 05 '17 at 15:42
  • @ChrisG The quotes make sense; however the issue is still not solved – Ajility Jan 05 '17 at 15:43
  • @SamiAji You're setting `window.onload` to the result of the call. You essentially have `window.unload = undefined;` –  Jan 05 '17 at 15:45
  • @ChrisG As I found at https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload, the onload event in triggered after the entire page has loaded.. What does unload have to do with this? My objective is to have this quote executed when the correct link is visited and the page has loaded. – Ajility Jan 05 '17 at 15:52
  • @SamiAji `window.onload = success;` would work. Setting it to `success()` will call the function right away, then set `window.onload` to the returned value, i.e. `undefined`, since the function doesn't return anything. –  Jan 05 '17 at 15:54

2 Answers2

0

I've tested is the inexistent constant you're using called success. As @Chris G said

Change the if for :

if ($alert == "success")
0

You need quotes around the texts and properly set window.onload:

<?php 
function GET($key) {
    return isset($_GET[$key]) ? $_GET[$key] : null;
}

$alert = GET('message');
echo "<script>window.onload = function () { ";
if ($alert == "success") echo "success();";
else if ($alert == "removed") echo "remove();";
echo " };</script>";
?>

If those two are all you need, you can also do this:

$alert = GET('message');
if ($alert == "success" || $alert == "remove") {
    echo "<script>window.onload = $alert;</script>";
}

Edit:
To clarify an issue from the comments: to set window.onload to a function, one cannot use

window.onload = myFunc();  // WRONG!

(This will call the function, then set window.onload to the result)

The correct way is:

window.onload = myFunc;
// OR
window.onload = function () {
  myFunc();
};
  • The first solution accomplished my objective, you are very much appreciated. However the second (simplified) solution did not work. – Ajility Jan 05 '17 at 15:58
  • There is one new issue with this code. When I visit projects.php I am redirected to projects.php?=removed. If I visit with ?message=success or ?message=removed, it works appropriately. If i visit a random key URL, such as ?message=fakeaddress, I see the correct page that I should see when visiting projects.php. Solved by surrounding the code with if ($alert != null) – Ajility Jan 05 '17 at 16:32
  • @SamiAji You have errors in your remaining code so you edit my answer...? Nope. You don't. Nothing in my code is capable of redirecting, and when I test my code without parameter it procudes ``, exactly as it should. –  Jan 05 '17 at 17:30
  • @SamiAji I should also note that the second, shorter version *does* work for me. Whatever the screw-up is, it's not in my code. You have to put the called function in an earlier ` –  Jan 05 '17 at 17:33
  • I am making no attempt to belittle you or be rude with you, please do not take it this way.. I edited the code with the solution that worked for me so that others with the same issue who come across this question may find a solution that fits. Thank you for your assistance.. – Ajility Jan 05 '17 at 17:38
  • @SamiAji I'm also not trying to be rude, but you are in no position to edit my answer, even if it *does* contain a mistake. If you think you found one, after thoroughly checking your own code first, post a comment and I will look into it. –  Jan 05 '17 at 18:56
  • As for others with the same issue: http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php –  Jan 05 '17 at 19:03
  • This is a user-driven content website. Collaboration makes the content accurate. – Ajility Jan 05 '17 at 20:44
  • @SamiAji I agree, as long as the collaborators know what they're doing. As for what SO is, it is the absolute last resort for advanced programming problems, after all other resources have been exhausted, with one idea at the core: to avoid clutter. Which means you have no idea what SO is. –  Jan 06 '17 at 16:24
  • You've got quite the God's complex! This isn't a 10,000 line program – Ajility Jan 11 '17 at 16:19
  • @SamiAji What exactly are you talking about? SIX other people voted down your question, keep that in mind. It contributes *nothing* to this site. –  Jan 11 '17 at 16:22
  • Seven total people with enough rep to make a public vote voted me down. The public rating does not include upvotes/downvotes made by users without enough rep. Since content here is user-generated, SO will not gain content if users here do not feel welcomed. This isn't a tutorial site, its an "I have a programming issue and I need some help resolving it" website. – Ajility Jan 11 '17 at 16:26
  • @SamiAji I still don't know what your point is. Your "programming issues" are total noob stuff, I'm sorry to say: forgetting quotes and not understanding how functions work. Precisely *not* SO material. SO doesn't need more of that, it needs way, way less of that. ---- Note that I don't have any issue with you, or the fact that you don't know what SO is for, but "fixing" my answer was simply one step too far. –  Jan 11 '17 at 16:32
  • Enjoy the rest of your day! – Ajility Jan 11 '17 at 16:38
  • @SamiAji Thanks for taking the high road, I like it much better down here. –  Jan 11 '17 at 16:40