简体   繁体   中英

How do I get $_POST value in php controller from php jquery ajax

this is my first time posting here but I can't seem to find an answer to the question I have. I have been using stack for years now but this is one of the first times where I just can't seem to find an answer. If I should be doing something differently please feel free to let me know.

I am dynamically populating buttons based on file names present in a directory. I need to send the name of the button clicked to my controller using jquery and ajax. I am using the following code to do this:

$currentDir = getcwd();
                $files = scandir($currentDir);
                $count = 0;
                foreach ($files as $name) {
                    if(preg_match("/^sma\w*/", $name)){
                        $workingFiles = substr($name, strpos($name, "_") + 1);
                        $availableActions = explode(".", $workingFiles);
                        $class_action = $availableActions[0];
                        $buttonName = $class_action;
                        $count ++;
                        ?>
                        <button id="<?php echo $class_action;?>" class="actionButtons"><? echo $buttonName; ?></button>
                            <script>
                                $(document).ready(function() {
                                    $("#<?php echo $class_action;?>").click(function(){

                                        var class_action = <?php echo $class_action;?>    
                                        $.ajax({ url: url + '?action=GetStatus&action=Execute',
                                            type: 'POST',
                                            data: JSON.stringify({action: "GetStatus", action: "Execute", actionItem: class_action}),
                                            contentType: 'application/json; charset=utf-8',
                                            success: function(output) {
                                                if(output){
                                                    console.log(output); 


                                                }
                                                else{
                                                    console.log("Nothing returned");

                                                }
                                            },
                                                error: function() {
                                                    console.log("error in the execute call");

                                                }
                                        });
                                    });
                                });
                        </script>
                        <?
                    }                        
                }                    

With a controller that looks like this:

if(isset($_GET['action'])){
$action = $_GET["action"];
switch($action){
    case "GetStatus":       
    _setContent();
    break;
    case "Execute": 
    if(isset($_POST['data'])){
        $data = json_decode($_POST['data']);
        echo $data;
    }
    break;

Ideas? Any feedback would be greatly appreciated!

Why do you have $(document).ready inside the loop?

Instead, you can use this below code:

 $(document).ready(function() { $(".actionButtons").click(function() { var class_action = $(this).attr('id') $.ajax({ url: url + '?action=GetStatus&action=Execute', type: 'POST', data: JSON.stringify({ action: "GetStatus", action: "Execute", actionItem: class_action }), contentType: 'application/json; charset=utf-8', success: function(output) { if (output) { console.log(output); } else { console.log("Nothing returned"); } }, error: function() { console.log("error in the execute call"); } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <?php $currentDir = getcwd(); $files = scandir($currentDir); $count = 0; foreach ($files as $name) { if(preg_match("/^sma\\w*/", $name)){ $workingFiles = substr($name, strpos($name, "_") + 1); $availableActions = explode(".", $workingFiles); $class_action = $availableActions[0]; $buttonName = $class_action; $count ++; echo "<button id="$class_action" class='actionButtons'>" . $buttonName. "</button>"; } } ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM