Execute php script inside a javascript script -
i've got problem using javascript (as i'm pretty new language)
my problem : i've got javascript script adds form fields when click on button, , change name incrementing it. data stored array. when i've saved filled fields, need them , show them <input type="text" value="<?php $myvalue[] ?>"/>
. problem is, need javascript file, , don't know how can interpret stuff inside php script.
here's i've tried :
var counter = 1; var limit = 10; var new_div = '<?php echo json_encode($arrayspecialite['+ counter +']); ?>'; function addinput(divname){ if (counter == limit) { alert('you cannot add more than' + counter + ' fields'); } else { var newdiv = document.createelement('div'); newdiv.innerhtml = 'specialité ' + (counter + 1) + ' <br><input type="text" name="specialite[]" id="specialite[]"" value="'+ new_div +'">'; document.getelementbyid(divname).appendchild(newdiv); counter++; } }
thank's in advance.
you can differents things:
in php return initial view, return response contains javascript hold data.
eg:
<script> var new_div = '<?php echo json_encode(array(1,2,3)); ?>' </script>
so if javascript run in same scope, or in inner scope have access variable.
or
make ajax request , read response server
xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { console.log(xmlhttp.responsetext); } } xmlhttp.open("post","myscript.php",true); xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded"); xmlhttp.send("name=shikaka&lastname=ventura");
when myscript.php receive request work (make sure escape values) , return response
<?php echo json_encode(array(1,2,3));
Comments
Post a Comment