php - $_GET JavaScript and AJAX... -
i relatively new php , (trying to) developing first ajax code...
i trying pass attribute select several options, each option own unique attribute "values" string.
the php file gets value , (for instance prints out). , generated file returned main html page.
here source code...
here javascript:
function showuser(myelement) { var str = myelement.options[myelement.selectedindex].getattribute("values"); if (str == "") { document.getelementbyid("myresponse").innerhtml = ""; return; } else { if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { document.getelementbyid("myresponse").innerhtml = xmlhttp.responsetext; } } xmlhttp.open("get","getuser.php?q="+encodeuricomponent(str),true); xmlhttp.send(); } }
my html following:
<form> <select name="users" onchange="showuser(this)"> <option values="">select person:</option> <option values="rasmus lerdorf">creator of php</option> <option values="linus torvalds">developed linux</option> <option values="dennis ritchie">developper of c</option> </select> </form> <br> <div id="myresponse"><b>person info listed here...</b></div>
my php is:
<?php $q = intval($_get['q']); echo $q; ?>
i 0 instead of text string, wrong code?
silly me,
i copied , pasted basic example tutorial , did not realize php code casting string sent on integer.
$q = intval($_get['q']);
the code should :
$q = $_get['q'];
on earlier stage of development indeed passing integer, code working.
the code returned 0 instead of error because intval of string 0 on failure.
writing down question here realized mistake...
Comments
Post a Comment