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.
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>
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.