Jump to content

Recommended Posts

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>

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.

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 by Barand

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.

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...

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.  :) 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.