tarquino Posted April 29, 2015 Share Posted April 29, 2015 I want to be able to check whether values exist in the php array without having to click the submit button to do those checks using jquery/ajax. when users enter an abbreviation in the text field want to be able to show that the brand exists (either vw or tyta) or not (as they type in the input box) and show the results in the carnamestatus div.I was following a tutorial from youtube, however it queried against a mysql database. I was wondering if this is possible using php arrays instead of mysql? I would be grateful if you could pick any faults in the code. The jquery is in the head section. the rest of the code is as follows: <?php $car = array() $car["vw"] = array( "name" => "volkswagen"); $car["tyta"] = array( "name => "toyota"); ?> the html code is as follows: <label for="carname">Car Code:</label> <input type="text" onblur="checkcar()" value="" id="carname" /> <div id="carnamestatus"></div> the checkcar() function checkcar(){ var u = _("carname").value; if(u != ""){ _("carname").innerHTML = 'checking ...'; var B = new XMLHttpRequest(); B.open("POST","check.php",true); / B.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); B.onreadystatechange = function() { if(B.readyState==4 && B.status== 200) { _("carnamestatus").innerHTML = B.responseText; } } var v = "carnamecheck="+u; B.send(v); } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/295940-instant-validation-using-php-arrays-using-ajax-or-jquery/ Share on other sites More sharing options...
Muddy_Funster Posted April 29, 2015 Share Posted April 29, 2015 The ajax request doesn't interact with the database, the PHP script does that and then generates an array that is then passed back to the ajax request (normaly in the form of JSON). so ajax doesn't care where PHP gets the data, just as long as there is a valid response sent from the PHP script, the core data can be hard coded into the PHP, read from a text file, come from an XML file, loaded in from a database, whatever really. Quote Link to comment https://forums.phpfreaks.com/topic/295940-instant-validation-using-php-arrays-using-ajax-or-jquery/#findComment-1510244 Share on other sites More sharing options...
Barand Posted April 29, 2015 Share Posted April 29, 2015 (edited) your code to create the array $car = array(); $car["vw"] = array( "name" => "volkswagen"); $car["tyta"] = array( "name" => "toyota"); will give you Array ( [vw] => Array ( [name] => volkswagen ) [tyta] => Array ( [name] => toyota ) ) It would be better and simpler to $car = array(); $car["vw"] = "volkswagen"; $car["tyta"] = "toyota"; giving Array ( [vw] => volkswagen [tyta] => toyota ) But why not just give them a dropdown menu of valid types to select from Edited April 29, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/295940-instant-validation-using-php-arrays-using-ajax-or-jquery/#findComment-1510248 Share on other sites More sharing options...
tarquino Posted April 29, 2015 Author Share Posted April 29, 2015 your code to create the array $car = array(); $car["vw"] = array( "name" => "volkswagen"); $car["tyta"] = array( "name" => "toyota"); will give you Array ( [vw] => Array ( [name] => volkswagen ) [tyta] => Array ( [name] => toyota ) ) It would be better and simpler to $car = array(); $car["vw"] = "volkswagen"; $car["tyta"] = "toyota"; giving Array ( [vw] => volkswagen [tyta] => toyota ) But why not just give them a dropdown menu of valid types to select from the reason is i am interested in learning a bit of ajax, i followed a few tutorials and while it queries using a database, i wanted to know what the process is (and know whether) arrays can be queried too. Quote Link to comment https://forums.phpfreaks.com/topic/295940-instant-validation-using-php-arrays-using-ajax-or-jquery/#findComment-1510250 Share on other sites More sharing options...
Muddy_Funster Posted April 29, 2015 Share Posted April 29, 2015 To make a closer facsimile of the type of array you get back from a db query you would probably want to have more along the lines of: $car = array(); $car[] = array('sCode'=>'vw', 'name'=>'volkswagen'); $car[] = array('sCode'=>'tyta', 'name'=>'toyota'); Then you can do the following: if(!isset($_POST['???']){ $return = ""; } else{ foreach($car as $key=>$row){ if ($_POST['???'] == $row['sCode']{ $return = $row['name']; } else{ $return = ""; } } echo $return; You should be using a get request, not a post request as you are getting info back from the server, not posting anything to it. Also I strongly suggest using jquery for ajax calls, it makes the whole coding much easier so you can quickly spot things like missing data variables being passed into the xhtml request... Quote Link to comment https://forums.phpfreaks.com/topic/295940-instant-validation-using-php-arrays-using-ajax-or-jquery/#findComment-1510257 Share on other sites More sharing options...
tarquino Posted April 30, 2015 Author Share Posted April 30, 2015 To make a closer facsimile of the type of array you get back from a db query you would probably want to have more along the lines of: $car = array(); $car[] = array('sCode'=>'vw', 'name'=>'volkswagen'); $car[] = array('sCode'=>'tyta', 'name'=>'toyota'); Then you can do the following: if(!isset($_POST['???']){ $return = ""; } else{ foreach($car as $key=>$row){ if ($_POST['???'] == $row['sCode']{ $return = $row['name']; } else{ $return = ""; } } echo $return; You should be using a get request, not a post request as you are getting info back from the server, not posting anything to it. Also I strongly suggest using jquery for ajax calls, it makes the whole coding much easier so you can quickly spot things like missing data variables being passed into the xhtml request... thanks for the advice. i am getting a grip on jquery and ajax at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/295940-instant-validation-using-php-arrays-using-ajax-or-jquery/#findComment-1510380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.