-4

I´ve got a javascript method that calls a php method to get some data in the array "availabletags". To prove if the function is even called, I added

availableTags[0] = "Test";

When I put it on my server to try it, nothings happens. So I think the javascript function isn´t called.

<link rel="stylesheet" href="js/demos.css">
<script type="text/javascript">
$(function() {
        var availableTags = new Array(400);
        availableTags[0] = "Test";
        availableTags = JSON.parse(<?php echo '"'.addslashes(include("/php/search_new.php")).'"';  ?>);
        for(var i=0;i<availableTags.length;i++){
            alert("<b>availableTags["+i+"] is  </b>=>"+availableTags[i]+"<br>");
            }
    }
</script>

</head>

<body>
        <input id="searchrecipes" type="text" name="searchrecipes" class="searchinput" style="margin-left: 850px; margin-top: 0px; width:170px; background: #fff url(images/search_icon.png) no-repeat 100%;" placeholder="Suchen..."></input>
        <input type="submit" name="buttonsenden" style="display:none;" value="" width: 5px></input>



</body>
</html>

Does anybody know my mistake?

Harald
  • 849
  • 3
  • 11
  • 15
  • You cannot call a PHP method from JavaScript. What happens is that the PHP is executed first, its result is inserted into your HTML/JavaScript code and then interpreted by the browser. Have a look at the created HTML, you most likely have a syntax or other error in your resulting JavaScript. – Felix Kling Nov 04 '11 at 10:10
  • Is your problem "Some PHP doesn't generate the HTML/JS I expect" or "A browser doesn't treat some HTML/JS in the way I expect"? Figure out which and then show us the relevant bits (i.e. all the PHP, the output you get, and an explanation of how it differs from what you expect or the output of the PHP (with no PHP in it) and an explanation of how the browser doesn't react in the way you expect). Don't show us some PHP (especially with bits missing!) and then tell us the browser doesn't handle it properly. – Quentin Nov 04 '11 at 10:11
  • It seems you have to clarify your question with what you actually want to do. And post the code the browser is getting, not your PHP code... – Felix Kling Nov 04 '11 at 10:24

1 Answers1

0

Learn and use AJAX to change your javascript array without to refresh your page: AJAX call looks like:

<script type='text/javascript'>
function yourFunction(str, typ)
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                myArray = xmlhttp.responseText; //here is the result
            }
        }
        xmlhttp.open("GET","myFile.php?param1="+str ,true); //here you lunch myFile 
        xmlhttp.send();
    }
</script>
profimedica
  • 2,716
  • 31
  • 41