Trying to use AJAX to grab parameter of function and pass to PHP codeHow do you pass a function as a parameter in C?Is there a better way to do optional function parameters in JavaScript?startsWith() and endsWith() functions in PHPSet a default parameter value for a JavaScript functionHow can I pass a parameter to a setTimeout() callback?Passing an array as a function parameter in JavaScriptjQuery Ajax POST example with PHPWhy shouldn't I use mysql_* functions in PHP?Pass a JavaScript function as parameterHow do I pass variables and data from PHP to JavaScript?

If every star in the universe except the Sun were destroyed, would we die?

Schrodinger's Cat Isn't Meant To Be Taken Seriously, Right?

The Green Glass Door, Revisited

What makes things real?

Can you pop microwave popcorn on a stove?

Furthest distance half the diameter?

Capacitors with same voltage, same capacitance, same temp, different diameter?

Why does 8 bit truecolor use only 2 bits for blue?

What makes an ending "happy"?

What's the biggest difference between these two photos?

Bit floating sequence

How to run NPCs with complicated mechanics?

Why can't some airports handle heavy aircraft while others do it easily (same runway length)?

How invisible hand adjusts stock prices if company is listed on multiple exchanges, under multiple currencies, and one of the currencies plunges?

What explains the Genie's fate?

When did computers stop checking memory on boot?

Can taking my 1-week-old on a 6-7 hours journey in the car lead to medical complications?

Problem with listing a directory to grep

Did "Dirty Harry" feel lucky?

What precisely does the commonly reported network hash rate refer to?

How do English-speaking kids loudly request something?

Bacteria vats to generate edible biomass, require intermediary species?

Poor management handling of recent sickness and how to approach my return?

Are fast interviews red flags?



Trying to use AJAX to grab parameter of function and pass to PHP code


How do you pass a function as a parameter in C?Is there a better way to do optional function parameters in JavaScript?startsWith() and endsWith() functions in PHPSet a default parameter value for a JavaScript functionHow can I pass a parameter to a setTimeout() callback?Passing an array as a function parameter in JavaScriptjQuery Ajax POST example with PHPWhy shouldn't I use mysql_* functions in PHP?Pass a JavaScript function as parameterHow do I pass variables and data from PHP to JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have an HTML input with a function and parmeter set to it like so



<input onclick='myFunc($count)' type='button' value='ClickMe'>;


I also have script references to JQuery and my script file



 <script src="jquery.js"></script>
<script src="myscript.js"></script>


Inside my myscript.js file I have the contents as such



function myFunc(x) 
alert(x);

$(document).ready(function()
$.ajax(
url: "myphp.php",
method: "post",
data: param1: "x" ,
dataType: "text",
success: function(strMessage)


)
)




Here is my myphp.php file



<?php

$param1 = $_GET['param1'];
echo $param1;
?>


As you can see in the javascript file, I have alert(x); to test that the function is grabbing the $count variable and so far that is working (the correct value appears in the alert box). However, I want to pass that x parameter to the PHP script but my PHP script won't echo anything, I assume the line where I have param1 is wrong. Any tips?










share|improve this question
























  • remove the quotes from data: param1: "x" , in your javascript and add alert(strMessage); inside of the success: function( strMessage ) curly braces.

    – JasonB
    Mar 28 at 7:03











  • I followed your instructions and removed the alert(x); function underr myFunc() and when I click the button I get an alert but nothing inside the alert. This would mean that either the parameter is not being passed in AJAX and/or PHP? It is kind of hard to test if AJAX is calling the PHP file from within the web browser.

    – whatitiswhatsup
    Mar 28 at 7:08


















0















I have an HTML input with a function and parmeter set to it like so



<input onclick='myFunc($count)' type='button' value='ClickMe'>;


I also have script references to JQuery and my script file



 <script src="jquery.js"></script>
<script src="myscript.js"></script>


Inside my myscript.js file I have the contents as such



function myFunc(x) 
alert(x);

$(document).ready(function()
$.ajax(
url: "myphp.php",
method: "post",
data: param1: "x" ,
dataType: "text",
success: function(strMessage)


)
)




Here is my myphp.php file



<?php

$param1 = $_GET['param1'];
echo $param1;
?>


As you can see in the javascript file, I have alert(x); to test that the function is grabbing the $count variable and so far that is working (the correct value appears in the alert box). However, I want to pass that x parameter to the PHP script but my PHP script won't echo anything, I assume the line where I have param1 is wrong. Any tips?










share|improve this question
























  • remove the quotes from data: param1: "x" , in your javascript and add alert(strMessage); inside of the success: function( strMessage ) curly braces.

    – JasonB
    Mar 28 at 7:03











  • I followed your instructions and removed the alert(x); function underr myFunc() and when I click the button I get an alert but nothing inside the alert. This would mean that either the parameter is not being passed in AJAX and/or PHP? It is kind of hard to test if AJAX is calling the PHP file from within the web browser.

    – whatitiswhatsup
    Mar 28 at 7:08














0












0








0








I have an HTML input with a function and parmeter set to it like so



<input onclick='myFunc($count)' type='button' value='ClickMe'>;


I also have script references to JQuery and my script file



 <script src="jquery.js"></script>
<script src="myscript.js"></script>


Inside my myscript.js file I have the contents as such



function myFunc(x) 
alert(x);

$(document).ready(function()
$.ajax(
url: "myphp.php",
method: "post",
data: param1: "x" ,
dataType: "text",
success: function(strMessage)


)
)




Here is my myphp.php file



<?php

$param1 = $_GET['param1'];
echo $param1;
?>


As you can see in the javascript file, I have alert(x); to test that the function is grabbing the $count variable and so far that is working (the correct value appears in the alert box). However, I want to pass that x parameter to the PHP script but my PHP script won't echo anything, I assume the line where I have param1 is wrong. Any tips?










share|improve this question














I have an HTML input with a function and parmeter set to it like so



<input onclick='myFunc($count)' type='button' value='ClickMe'>;


I also have script references to JQuery and my script file



 <script src="jquery.js"></script>
<script src="myscript.js"></script>


Inside my myscript.js file I have the contents as such



function myFunc(x) 
alert(x);

$(document).ready(function()
$.ajax(
url: "myphp.php",
method: "post",
data: param1: "x" ,
dataType: "text",
success: function(strMessage)


)
)




Here is my myphp.php file



<?php

$param1 = $_GET['param1'];
echo $param1;
?>


As you can see in the javascript file, I have alert(x); to test that the function is grabbing the $count variable and so far that is working (the correct value appears in the alert box). However, I want to pass that x parameter to the PHP script but my PHP script won't echo anything, I assume the line where I have param1 is wrong. Any tips?







javascript php jquery ajax function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 6:59









whatitiswhatsupwhatitiswhatsup

12 bronze badges




12 bronze badges















  • remove the quotes from data: param1: "x" , in your javascript and add alert(strMessage); inside of the success: function( strMessage ) curly braces.

    – JasonB
    Mar 28 at 7:03











  • I followed your instructions and removed the alert(x); function underr myFunc() and when I click the button I get an alert but nothing inside the alert. This would mean that either the parameter is not being passed in AJAX and/or PHP? It is kind of hard to test if AJAX is calling the PHP file from within the web browser.

    – whatitiswhatsup
    Mar 28 at 7:08


















  • remove the quotes from data: param1: "x" , in your javascript and add alert(strMessage); inside of the success: function( strMessage ) curly braces.

    – JasonB
    Mar 28 at 7:03











  • I followed your instructions and removed the alert(x); function underr myFunc() and when I click the button I get an alert but nothing inside the alert. This would mean that either the parameter is not being passed in AJAX and/or PHP? It is kind of hard to test if AJAX is calling the PHP file from within the web browser.

    – whatitiswhatsup
    Mar 28 at 7:08

















remove the quotes from data: param1: "x" , in your javascript and add alert(strMessage); inside of the success: function( strMessage ) curly braces.

– JasonB
Mar 28 at 7:03





remove the quotes from data: param1: "x" , in your javascript and add alert(strMessage); inside of the success: function( strMessage ) curly braces.

– JasonB
Mar 28 at 7:03













I followed your instructions and removed the alert(x); function underr myFunc() and when I click the button I get an alert but nothing inside the alert. This would mean that either the parameter is not being passed in AJAX and/or PHP? It is kind of hard to test if AJAX is calling the PHP file from within the web browser.

– whatitiswhatsup
Mar 28 at 7:08






I followed your instructions and removed the alert(x); function underr myFunc() and when I click the button I get an alert but nothing inside the alert. This would mean that either the parameter is not being passed in AJAX and/or PHP? It is kind of hard to test if AJAX is calling the PHP file from within the web browser.

– whatitiswhatsup
Mar 28 at 7:08













3 Answers
3






active

oldest

votes


















1
















In your AJAX call you are using a POST method, so in order to catch the variable in PHP you need to access it from $_POST:



<?php
$param1 = $_POST['param1'];
echo $param1;
?>





share|improve this answer
































    0
















    You're making the XHR with POST method




    method: "post",




    and youre looking for it in the GET array




    $_GET['param1'];




    Change either to post or get (keeping in mind your scenario) and you should be good imo.



    read more here to know about the different methods of sending http requests: https://www.guru99.com/php-forms-handling.html






    share|improve this answer
































      0
















      You are using post method in AJAX but trying to grab the value in $_GET which is wrong.



      In your PHP code, just replace $_GET['param1'] with $_POST['param1'] and it works.



      or If you like to use $_GET in your PHP code then change the method in AJAX or use $.get. Examples can be found on W3School.
      https://www.w3schools.com/jquery/jquery_ajax_get_post.asp






      share|improve this answer



























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );














        draft saved

        draft discarded
















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391794%2ftrying-to-use-ajax-to-grab-parameter-of-function-and-pass-to-php-code%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1
















        In your AJAX call you are using a POST method, so in order to catch the variable in PHP you need to access it from $_POST:



        <?php
        $param1 = $_POST['param1'];
        echo $param1;
        ?>





        share|improve this answer





























          1
















          In your AJAX call you are using a POST method, so in order to catch the variable in PHP you need to access it from $_POST:



          <?php
          $param1 = $_POST['param1'];
          echo $param1;
          ?>





          share|improve this answer



























            1














            1










            1









            In your AJAX call you are using a POST method, so in order to catch the variable in PHP you need to access it from $_POST:



            <?php
            $param1 = $_POST['param1'];
            echo $param1;
            ?>





            share|improve this answer













            In your AJAX call you are using a POST method, so in order to catch the variable in PHP you need to access it from $_POST:



            <?php
            $param1 = $_POST['param1'];
            echo $param1;
            ?>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 28 at 7:05









            Diego RosalesDiego Rosales

            7395 silver badges16 bronze badges




            7395 silver badges16 bronze badges


























                0
















                You're making the XHR with POST method




                method: "post",




                and youre looking for it in the GET array




                $_GET['param1'];




                Change either to post or get (keeping in mind your scenario) and you should be good imo.



                read more here to know about the different methods of sending http requests: https://www.guru99.com/php-forms-handling.html






                share|improve this answer





























                  0
















                  You're making the XHR with POST method




                  method: "post",




                  and youre looking for it in the GET array




                  $_GET['param1'];




                  Change either to post or get (keeping in mind your scenario) and you should be good imo.



                  read more here to know about the different methods of sending http requests: https://www.guru99.com/php-forms-handling.html






                  share|improve this answer



























                    0














                    0










                    0









                    You're making the XHR with POST method




                    method: "post",




                    and youre looking for it in the GET array




                    $_GET['param1'];




                    Change either to post or get (keeping in mind your scenario) and you should be good imo.



                    read more here to know about the different methods of sending http requests: https://www.guru99.com/php-forms-handling.html






                    share|improve this answer













                    You're making the XHR with POST method




                    method: "post",




                    and youre looking for it in the GET array




                    $_GET['param1'];




                    Change either to post or get (keeping in mind your scenario) and you should be good imo.



                    read more here to know about the different methods of sending http requests: https://www.guru99.com/php-forms-handling.html







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 28 at 7:08









                    HishaamHishaam

                    214 bronze badges




                    214 bronze badges
























                        0
















                        You are using post method in AJAX but trying to grab the value in $_GET which is wrong.



                        In your PHP code, just replace $_GET['param1'] with $_POST['param1'] and it works.



                        or If you like to use $_GET in your PHP code then change the method in AJAX or use $.get. Examples can be found on W3School.
                        https://www.w3schools.com/jquery/jquery_ajax_get_post.asp






                        share|improve this answer





























                          0
















                          You are using post method in AJAX but trying to grab the value in $_GET which is wrong.



                          In your PHP code, just replace $_GET['param1'] with $_POST['param1'] and it works.



                          or If you like to use $_GET in your PHP code then change the method in AJAX or use $.get. Examples can be found on W3School.
                          https://www.w3schools.com/jquery/jquery_ajax_get_post.asp






                          share|improve this answer



























                            0














                            0










                            0









                            You are using post method in AJAX but trying to grab the value in $_GET which is wrong.



                            In your PHP code, just replace $_GET['param1'] with $_POST['param1'] and it works.



                            or If you like to use $_GET in your PHP code then change the method in AJAX or use $.get. Examples can be found on W3School.
                            https://www.w3schools.com/jquery/jquery_ajax_get_post.asp






                            share|improve this answer













                            You are using post method in AJAX but trying to grab the value in $_GET which is wrong.



                            In your PHP code, just replace $_GET['param1'] with $_POST['param1'] and it works.



                            or If you like to use $_GET in your PHP code then change the method in AJAX or use $.get. Examples can be found on W3School.
                            https://www.w3schools.com/jquery/jquery_ajax_get_post.asp







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 28 at 7:11









                            Sami Ahmed SiddiquiSami Ahmed Siddiqui

                            1,8651 gold badge9 silver badges20 bronze badges




                            1,8651 gold badge9 silver badges20 bronze badges































                                draft saved

                                draft discarded















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391794%2ftrying-to-use-ajax-to-grab-parameter-of-function-and-pass-to-php-code%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript