javascript - What is wrong with my ajax call to php file in data section? -


so able php value javascript variable don't think script.js finding it. there of course alot of files missing sake of keeping question clean problem script.js schoolid undefined instead of actual value when console.log schoolid value of "1"(first page has id 1 works).

header.php :

<?php  //gets current page id $schoolofficialid = $specificschool[0]["id"];  ?> <head>     <script type="text/javascript" src="js/script.js"></script> </head> <body>     <input type="hidden" id='schoolofficialid' value="<?php echo $schoolofficialid;?>"/>     <script type="text/javascript">         var schoolid = $('#schoolofficialid').val();     </script> </body> 

script.js: ajax call here note: in ajax know data having trouble finding schoolid header.php send php file.

// execute ajax query push id of page load_more.php             $.ajax({                 url: 'load_more.php',                 type: 'post',                 data: {schoolid:schoolid},                 success:function(data){                     console.log("working");                 }             }); 

my load_more.php:

if (isset($_post['schoolid'])) {     echo "yes"; }else {     echo "nope"; }//keeps echoing nope because schoolid not received 

the problem have script running before declaration.

<script type="text/javascript" src="js/script.js"></script> in head, var schoolid = $('#schoolofficialid').val(); @ bottom of page.

include script after declaring schoolid.

<head> </head> <body>     <input type="hidden" id='schoolofficialid' value="<?php echo $schoolofficialid;?>"/>     <script type="text/javascript">         var schoolid = $('#schoolofficialid').val();     </script>     <script type="text/javascript" src="js/script.js"></script> </body> 

p.s. despite other answers, data: {schoolid:schoolid}, fine unless keyname conflicts reserved word.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -