last day (14 days later) » 

2:20 AM
2
A: How to call a php script/function on a html button click

Priyank Just try this: <button type="button">Click Me</button> <p></p> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $.ajax({ type: 'POST', url: 'script.php', success: function(data...

 
@Daniel Spajic : try this one,may this will help you!!!
 
why do you pass in "data" into the function, I understand everything else except that
 
@DanielSpajic : when the ajax return with text "You win",it will appear in <p> tag,the tag which i wrritten after button
@DanielSpajic : you can try once then you understand thoroughly
 
what am i supposed to pass in as "data"
 
@DanielSpajic : mo need to pass any things.you just put my first code in index.php and second code in your script.php thats it.and then click the button.it 'll automatically work.just try once.
 
2:20 AM
I did but it didn't work, im pretty sure you have to pass something as "data"
 
@DanielSpajic can you give your email id.i'll send you my file and explain you.it's very easy concept but very important**
@DanielSpajic : hi i sent you my file.just put this two files in your localhost and run
 
Thanks it worked, however I don't understand how it worked. you used 'url: 'script.php'' to link the file which contained 'echo "You win";' however how does this affect the jquery code? All you did was link a file, how did you actually get the echoed string from script.php
 
@DanielSpajic : hmm now i'll explain you
 
I see that no matter what you pass in it still works, you just used "data". Why is that? I tried removing the text in there, and it didn't return anything
 
@DanielSpajic : see in line no.9 in index.php,you click the button.which call your ajax with the help of this "$("button").click(function()" which redirect you to script.php with the help of url in ajax.then in script.php we echo and return to our "success: function(data) {" in ajax where you write the return text i.e. "you win" in <p> tag with the help of "$("p").text(data);"
@DanielSpajic : "data" is just a kind of variable you can give any name
@DanielSpajic Hey did you understand my explanation?
 
2:20 AM
Yes I did, I just dont get how "data" even applies to anything since you never even used it. Normally when you put an argument into a function, you pass it in later. E.g : function the_function (argument) { } the_function(stuff);
Also how do you control the returned data from script.php? at the moment theres only an echoed string, however how do you specify what you want to return from there. For example if i echo 2 strings, how do i return one of them instead of the other
 
@DanielSpajic : concept is function accept the argument which is return from your script.php by echo.if you have two echo string,it will return both string.
@DanielSpajic : can you upvote my answer!!!if my answer is helpful for you
 
is there a way to choose which returned data you want to accept?
 
@DanielSpajic : just put echo before string you want to return. for example: "You win"; echo "You loss"; so now you get only "You loss"
 
Yes but for complex scripts you want to be able to dynamically select your output, so whats a way to access that returned data and select which one you want to use?
 
@DanielSpajic : then you try my another answer, which is post in same
 
2:21 AM
hey ive tried using that script in my own project, however its not working for some reason
 

last day (14 days later) »