1

I'm having a problem with php javaScript echo. I wanna fire a javaScript functions for buttons which are dynamically created by using echo.

When i test it without echo, functions are using properly. But when I create it using echo, it doesn't work.

Here is the echo in pages.php:

$cont.='<div style="border-bottom: 1px solid #A2DBFF; padding-bottom: 25px; padding-top: 20px; margin-bottom: 10px; margin-left: 25px;">';
$cont.='<div class="SUBH">
        <input type="text" id="txtNEWHEADING" name="txtSubNam" placeholder="The new heading"  />
    </div>';
$cont.='<div class="PTAG">
        <p>
            <textarea rows="5" id="disNEWCONT" cols="70" placeholder="add a discription here.." name="txtCont"></textarea>
        </p>
    </div>';
$cont.='<div class="IMGTAG">
        <input type="file" id="imgNewIMAGE" />
        <button class="btnSav"  type="submit" onclick="SaveContent()" id="btnNEWCONTENT" style="margin-left: 300px;" />save</button>
    </div>';
$cont .= '</div>';

And the javaScript is here:

$cont .= '
    <script type="text/javascript">

        $("#btnNEWCONTENT").click(function(){
            alert("hi there..");
            $.ajax({ url: \'ajax.php\',
                data: {action: \'test\'},
                type: \'post\',
                success: function(data) {
                    alert(data);
                }
            });
        });     
    </script>
    ';
echo $cont;

Please help.

6
  • When did jQuery library load? Commented Sep 29, 2013 at 3:23
  • There is no reason that 2 identical HTML pages would work differently, can you post those 2 pages? Commented Sep 29, 2013 at 3:24
  • PHP is just generating the output sent to the browser. View Source in your browser to see what it output, and see if you can spot where it is not what you wanted. Commented Sep 29, 2013 at 3:24
  • You also may want to read up on HEREDOC strings. Commented Sep 29, 2013 at 3:26
  • Can you show the result output on browser? Commented Sep 29, 2013 at 3:36

1 Answer 1

2

This code is working for me ,added jquery between head , and wraped javascript into function:

<html>
<head>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
</head>
<body>
<?php

 $cont='<div style="border-bottom: 1px solid #A2DBFF; padding-bottom: 25px; padding-top: 20px; margin-bottom: 10px; margin-left: 25px;">';
    $cont.='<div class="SUBH">
                <input type="text" id="txtNEWHEADING" name="txtSubNam" placeholder="The new heading"  />
            </div>';
    $cont.='<div class="PTAG">
               <p>
                  <textarea rows="5" id="disNEWCONT" cols="70" placeholder="add a discription here.." name="txtCont"></textarea>
              </p>
            </div>';
    $cont.='<div class="IMGTAG">
              <input type="file" id="imgNewIMAGE" />
              <button class="btnSav"  type="submit" onclick="SaveContent()" id="btnNEWCONTENT" style="margin-left: 300px;" />save</button>
            </div>';
$cont.='</div>';

$cont.='
    <script type="text/javascript">
    function SaveContent(){
        $("#btnNEWCONTENT").click(function(){

            alert("hi there..");
            $.ajax({ url: \'ajax.php\',
             data: {action: \'test\'},
             type: \'post\',
             success: function(data) {
                 alert(data);
             }
        });

    });    } 
    </script>
    ';
    echo $cont;
?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

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.