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.
-
When did jQuery library load?Teddy– Teddy2013年09月29日 03:23:47 +00:00Commented Sep 29, 2013 at 3:23
-
There is no reason that 2 identical HTML pages would work differently, can you post those 2 pages?Sébastien– Sébastien2013年09月29日 03:24:11 +00:00Commented 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.IMSoP– IMSoP2013年09月29日 03:24:33 +00:00Commented Sep 29, 2013 at 3:24
-
You also may want to read up on HEREDOC strings.mario– mario2013年09月29日 03:26:03 +00:00Commented Sep 29, 2013 at 3:26
-
Can you show the result output on browser?edisonthk– edisonthk2013年09月29日 03:36:16 +00:00Commented Sep 29, 2013 at 3:36
1 Answer 1
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>
answered Sep 29, 2013 at 4:01
Charaf JRA
8,3551 gold badge36 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default