0

In my PHP file I have a variable $file = "abc". How would I create a button in HTML to run a function in PHP passing in $file as an argument? I saw that I need to do this using jQuery/AJAX. For the sake of simplicity, let's just say I want to echo $file.

HTML/JS

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.js"></script>
<input type="submit" class="button" name="Del" value="Del" />
<script type="text/javascript">
$(document).ready(function(){
    $('.button').click(function(){
        var clickBtnValue = $(this).val();
        var ajaxurl = 'ajax.php',
        data =  {'action': clickBtnValue};
        $.post(ajaxurl, data, function (response) {
            console.log(response);
            //alert("action performed successfully");
        });
    });

});
</script>

ajax.php

<?php
if (isset($_POST['action'])) {
    Del();
}

function Del($file) {
    echo $_POST['action'];
    echo $file;
    exit;
}
?>

I am unsure of how to modify this to pass in a php argument ($file) through AJAX.

5
  • Where is "file" coming from? Is it a text field in the form? Is it a variable in PHP? You state that it is in PHP, which means there is no meaning of putting it to the client and retrieving it back.. Commented Nov 25, 2014 at 20:23
  • $file is a PHP variable I set earlier before I run the ajax post. I want to pass $file into my Del() function. For testing purposes I just want to echo it and see if I can see it in the console.log Commented Nov 25, 2014 at 20:28
  • Do you mean earlier than your isset in ajax.php? Commented Nov 25, 2014 at 20:30
  • I initialize $file before I even make the AJAX call. Ok so, I have a variable $file that I am trying to pass into the Del() function. How would I pass $file into my AJAX call to run Del($file) Commented Nov 25, 2014 at 20:33
  • 1
    Answer by Eric Ping is what you need then :) Commented Nov 25, 2014 at 20:34

2 Answers 2

2

In your HTML/JS:

data =  {'action': clickBtnValue, 'file' : '<?php echo $file?>'};

In your ajax.php:

if (isset($_POST['action'])) {
    Del($_POST['file']);
}

This is a very simple example but you can probably adapt it to your needs. Note that there are a ton of potential security implications with this approach, depending on what you're actually doing, so be careful and clean any input in $_POST.

Sign up to request clarification or add additional context in comments.

1 Comment

There we go. My issue was i did not wrap the <?php echo..?> in single quotes.
0

You could probably pass in the value into a <span id="myFile"></span> that you could put right before the closing </body> tag, and then use jQuery to get the value and then remove it right after. I'm not saying it's the best or safest approach, but it's worked in the past for me with simple things.

HTML

    ...
    <span id="myFile"><?php echo $file; ?></span>
</body>
    ...

JS

$(document).ready(function(){

    $('.button').click(function(){

        //Get the text and remove it
        var myFile = $("#myFile").text();
        $("#myFile").remove();

        var clickBtnValue = $(this).val();
        var ajaxurl = 'ajax.php',
        data =  {action: clickBtnValue, file: myFile};
        $.post(ajaxurl, data, function (response) {
            console.log(response);
            //alert("action performed successfully");

            //Append it back in case you need to use it again. Optional
            $("body").append("<span id='myFile'></span>");
        });
    });
});

I took the quotes off of action.

PHP

<?php
    if (isset($_POST['action'])) {
        Del($_POST['file']);
    }

    ...
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.