0

I am totally new in php, so my opology if this question seems weird to you.

I have one php file (index.php) something like,

echo "
    <div>  
        <FORM name='myform' id='myform' method='post' action=''>

        // I fetch data from mysql and populate dropdown list. Tha't work fine.
        // Then I have one submit button when I click on that

        echo "<button id='showButton' type='submit'> Show </button>"; 

        </FORM>
";

Then I have one process.js file something like,

$(document).ready(function() {

    $('#showButton').click(function () {

        // Here I make one AJAX call to php file (fetch_more_data.php), which fetch more data from database
        // This also works fine

    });

});

In fetch_more_data.php I fetch more data and display in table using

echo "
    <script type = 'text/javascript' src = 'edit.js'></script>

    <table ...>

    <td>
        <button id="myButton"></button>
    </td>

    </table>

";

This is also work fine. but I want to edit one table cell and for that I need to write some java script code. I want to write on click function for myButton in Javascripr, for that I have written on edit.js file,

$(document).ready(function() {

    $('#myButton').click(function () {

        alert('Hello');

    });

});

The problem is $('#myButton').click(function () never called. I have spent long time but being a beginner my search options are limited. I would appriciate if someone solve this problem.

Regards,

10
  • 1
    please wrap your code up propperly before i think of an answer Commented Nov 21, 2014 at 12:32
  • you cannot use echo within the contents of another echo Commented Nov 21, 2014 at 12:33
  • You're using jquery.. stackoverflow.com/questions/10508319/… here's an example: jsfiddle.net/gdoron/WTzKR Commented Nov 21, 2014 at 12:34
  • make sure your edit.js is being read by your fetch_more_data.php file Commented Nov 21, 2014 at 12:36
  • ID's are unique so you may have some conflict there, add it as a class i.e. class="editButton" then call it like $('.editButton').click(function () //rest of your code Commented Nov 21, 2014 at 12:36

4 Answers 4

0

try calling it like this:

$(document).ready(function(){
  $(document).on('click', '#myButton', function(){
    alert('hi');
  });
});

hope this helped

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

Comments

0

try with

$('#myButton').live('click',function () {

alert('Hello');

});

});

1 Comment

Hi, Thanks for reply. Can I see if myButton callback function is called in firebug or not?
0

Try this:

$(document).ready(function() {

 $(document).on("click", "#myButton", function () {

alert('Hello');

 });
});

Since the html is being added to the DOM dynamically, you need to handle it using Event Delegation in jQuery

1 Comment

Thanks for comments, it seems I have no connect between fetch_more_data.php and edit.js. Ihave already inclduing echo "<script type = 'text/javascript' src = 'edit.js'></script>"; in fetch_more_data.php but no effect
0

I think the problem is that you are not loading the jQuery

Download the jquery on the same folder where is your php file and add this line in your code

<script src="jquery-1.11.0.min.js"></script>

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.