-2

I hate to ask questions like that, but i have been searching for quite a while, and i didn't find what i am looking for in either google or stackoverflow yet. I am looking for a way to show a short message on button click.
I know about alert('message'); but what i am looking for is a lot different.
When the button is clicked i want to :

  • Not reload the page.
  • Display a very short 1-2 line message next to the button that was clicked, depending on the result of a php function.
  • Excecute php function

One example is stack-overflow's message you get when you try to upvote your own question/answer/comment.
To decide which message you get, i will have to run a php function ( which i already have made ) and depending on the return value of that function i will either show message A , or message B. Any suggestions/guidance to this matter is welcome.

rez
  • 107
  • 2
  • 13
  • Can you please post up any code you have written so far. – Raad Oct 02 '14 at 09:04
  • So far i have only been learning php, and basic html coding. What i am trying to do is simply show a message next to a button, when that button is clicked, depending on the result of a php function and excecute another php function. Posting the code i made won't help, since it has nothing to do with the actual question. Which is sending a pop-up message. I don't know how to code something like this. I suspect it is very simple though – rez Oct 02 '14 at 09:08
  • @AbdulJabbar if you see the stackoverflow message on voting up your own comment , it has a small (x) button on top right corner , and also , if you click anywhere on it it closes, i dont really mind how the message will work, but id rather it to stay there , and you closing it manually , thats really minor though, im looking for the really base functionality of it – rez Oct 02 '14 at 09:11
  • If you want to display a message depending on a PHP script, you have to use AJAX : it will go find your PHP script, and return its value wherever you want on the page, without reloading it. You should learn difference between server-side and client-side language. – enguerranws Oct 02 '14 at 09:20
  • @enguerranws i already got that by now enguerranws. however my question remains focused on the point: `display a short message next to the button that was clicked` i already knew about ajax before i asked this question – rez Oct 02 '14 at 09:24

4 Answers4

2

I think the following functionality of TwitterBootstrap is what you're looking for:

See it working here

You can reposition it infront of your button and also style it according to your needs.

Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
  • haha, you're welcome.. there are also plenty of other custom tooltip libraries available out there. I got the hint that you're looking for the tooltip from your message regarding "message shown on voting up your own comment on stackoverflow" – Abdul Jabbar Oct 02 '14 at 09:29
1

You will have to learn ajax ( which you can do quite easily with jquery ). Ajax allow you to send data with javascript/jquery to a server ( php script ) and get a response without reloading the page http://api.jquery.com/jquery.post/

$.ajax({
type: "POST",
url: url, 
data: data,
success: success,
dataType: dataType
});

this code will call your php script (url), send data ( for example a button id or whatever ) and will return data from server ( success).

oligan
  • 634
  • 6
  • 18
  • 1
    the main question i have from your answer is: how will i actually show a small message , next to my button ? – rez Oct 02 '14 at 09:13
  • just create an empty div responseDiv in your html , and then after this code do something like responseDiv.html(success); With success the message from your php code ( echo $message in php) – oligan Oct 02 '14 at 09:14
  • there are plenty of examples http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – oligan Oct 02 '14 at 09:16
  • i think your answer is super general though, no ? how will show the message `next to my button` like a pop-up message, i don't think creating an empty div covers my question – rez Oct 02 '14 at 09:16
  • if you don't know .html() function in jquery i guess you have to learn some more, its very basic stuff – oligan Oct 02 '14 at 09:19
  • If you wanna somehting next to your button, then I guess we need to see your HTML to provide a full answer. – enguerranws Oct 02 '14 at 09:21
  • i mean.. if it was basic stuff, people woulda already posted working examples, something like when you click `submit` you see `successful submit` right next to the submit button . this is the basic functionality of what i am trying to do here , plus , excecuting a `` php function – rez Oct 02 '14 at 09:22
  • 1
    Sometimes when it seems obvious, we really can't help : you say you know about Ajax, and you don't know about .click() and .html() method. It's a bit confusing. – enguerranws Oct 02 '14 at 09:30
1

To display a message next to your button, assuming you're using jQuery (as all lazy people) :

$('#your-button').click(function(){
    $(this).next().html('Hello World :) ');
});

This basically will find the element right next to the button (here #your-button) you clicked on, empty its content and replace it with Hellow World :).

If you need to display a message depending on a PHP script, you need AJAX. It will be something like this :

$('#your-button').click(function(){
    $.get( "myscript.php", function( data ) {
       $(this).next().html(data);
       alert( "Load was performed." );
    });       
});
enguerranws
  • 8,087
  • 8
  • 49
  • 97
0

IN this case AJAX is your friend You can go to this link and find the answer by BalusC

Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69