0

okay here is my code :

var co = 0;
var comp = '';
<?php $i = 0;?>
while (co < <?php echo $db->count_rows(); ?>)
{
    if ((parseInt(value) >= <?php echo $mfr[$i] ?>) && (parseInt(value) <= <?php echo $mto[$i] ?>))
    {
        comp = 'T';
        break;
    }
    co++;
    <?php $i++; ?>
}

i'm still learning about this whole php and javascript thing, and i know there are many things that i still had to work to to improve my understanding to this both language. that's why i really need your help in this

i'm trying to get the while iteration to work so i can compare the variable from javascript with variable from php which took the value from database. the php variable ('$mfr' and '$mto'), as you can see, is an array. now i want it to look at every element of both and if it meets the condition then it will update the variable 'comp' and stop the whole while iteration

but the problem is the '$i' variable doesn't do the iteration thing. it runs only once so my '$mfr' and '$mto' value doesn't go anywhere. how can i fix this so i can compare the javascript value with the php '$mfr' and '$mto' value?

your help would be much appreciated, thank you :)

EDIT

well, it is actually a function of custom validation for jqgrid. and i do know that php is a server-side and javascript is a client-side language theoretically, though i don't really know it is practically

what i'm actually trying to do is when a user input a value and submit it, the system will check whether the value that was entered are between value of 'fromid' and 'toid' column of a table in database

here is my full code of the function

function checkid(value)
{
var co = 0;
var comp = '';
<?php $i = 0;?>
while (co < <?php echo $db->count_rows(); ?>)
{
    if ((parseInt(value) >= <?php echo $mfr[$i] ?>) && (parseInt(value) <= <?php echo $mto[$i] ?>))
    {
        comp = 'T';
        break;
    }
    co++;
    <?php echo $i++; ?>
}

if (comp != 'T')
{
    return [true, "", ""];
}
else
{
    return [false, "Value entered is already between a range. Please try again!", ""];
}
}

while this is how i got the '$mfr' and '$mto' variable

<?php
$db=new database($dbtype, $dbhost, $database, $dbuser, $dbpassword, $port, $dsn);
$db->query("select fromid, toid from CORE_ID");
$i = 0;
while($row = $db->get_row())
{
$mfr[$i] = $row[fromid];
$mto[$i] = $row[toid];
$i++;
}
?>

if theres any better way to do this, then please do tell me

R_A_P
  • 5
  • 1
  • 3

3 Answers3

3

Typically, PHP is for server side logic and JS is for client side logic. If you want to send a value from JS to be processed in PHP, you'll probably need to use something like AJAX, which jQuery makes pretty easy with jQuery.ajax().

Getting the client value to be processed is the difficult part. Once you can do that, rewriting your code logic in full PHP should not be difficult.

EDIT: If I'm misunderstanding where variable value comes from, please say so!

EDIT 2: It looks like you want to have client input compared to server side data. JS will not have access to your PHP variables unless they are specifically sent there. Likewise, you can send your JS value to the server for validation in the PHP.

In your case, you could use JSON to send send the JS your validation dates. Assuming you don't have too many dates, it will probably be faster than sending a value to the server and waiting for a response.

I found a good example of using JSON at another post. You can send an array like:

<?php
     $xdata = array(
          'foo'    => 'bar',
          'baz' => array('green','blue')
     );
?>
<script type="text/javascript">
    var xdata = <?php echo json_encode($xdata); ?>;

    alert(xdata['foo']);
    alert(xdata['baz'][0]);

    // Dot notation can be used if key/name is simple:
    alert(xdata.foo);
    alert(xdata.baz[0]);
</script>

For your code, you could put $mfr and $mto into a single 2D array. Here is how your new JS might look, assuming xdata contains $mfr and $mto:

function checkid(value) {
  var co = 0, comp = '', i = 0, nrows = xdata.mfr.length; 
  while (co < nrows) {
    if ((parseInt(value) >= xdata.mfr[i]) && (parseInt(value) <= xdata.mto[i])) {
      comp = 'T';
      break;
    }
    co++;
    i++;
  }

  if (comp != 'T') {
    return [true, "", ""];
  } else {
    return [false, "Value entered is already between a range. Please try again!", ""];
  }
}
Community
  • 1
  • 1
maaachine
  • 801
  • 1
  • 7
  • 12
  • not really. instead i'm trying to process it in javascript so it can be said that i send the value from php to js not the other way around. i hope my more explanation can make you understand more of what i want to do – R_A_P May 03 '13 at 08:02
  • Updated with some example code. I didn't test it, and I tried to leave it as similar to yours as possible. – maaachine May 03 '13 at 16:13
  • thank you so much, you really solved my problem :D your code actually doesn't really work but i get the logic there so i edit it and solved the problem. but all that thanks to you :D so once again, a big big thank you for helping me solving this. GBY :D – R_A_P May 06 '13 at 04:15
0

You have a loop in your Javascript, not your PHP. So the PHP is only going to get executed once. You need to rethink your approach. Without knowing what the script is supposed to actually achieve it's difficult to provide working code, but you at least need to put the loop into the PHP instead of the Javascript.

Mark Parnell
  • 9,175
  • 9
  • 31
  • 36
  • yes that is what i'm trying to do. i'm looping it in my javascript, not php. the php part is just to get data from database. but then how i transfer that php value so it can be read in the javascript. and it's not just a usual variable because it is an array. my problem is the counter for php part isn't countering. so my array data of the php part is stuck in it's first element so i can't compare it with value that i entered. now how would you suggest me to fix this? – R_A_P May 03 '13 at 07:59
0

Before I can answer you should understand what's going on exactly:

PHP is being executed on the server, then sends back the result (HTML and Javascript) to the client (the browser).

Javascript is being executed on the client side. So this only starts after PHP is completely done. For this reason you can't mix Javascript and PHP.

Check the source of the page, then you'll see exactly what the server returns (what HTML/Javascript PHP generates) and you'll get a better insight of what happens.

Now, if you understand this, you may be able to solve your own problem. I don't exactly know what you want to do, but I can advice you that if you need Javascript to check values from the database, you should generate Javascript using PHP that defines these values in the Javascript like for example this:

var my_js_var = <?=$my_php_var?>
var another_var = <?=$another_one?>

Now they are defined in Javascript and you can use them (and check them).

When you have a large database it can become inefficient to do it like this and you might want to look into a technology called AJAX, which allows Javascript to do a request to the server and get back data from a PHP script.

You would also want to do this if there's data involved you don't want to be publicly viewable (because everyone can look into the source of your page.

gitaarik
  • 42,736
  • 12
  • 98
  • 105
  • i haven't tried java because i never even touched it so i'm trying to do this in jqgrid or javascript or php kind of way. that is if there is a way to do. is that how to transfer value from php to javascript? then what if the php variable is an array? how it can be done? – R_A_P May 03 '13 at 07:58
  • First of all: Java is not the same as JavaScript. Second: you can't transfer data from PHP to JavaScript as I said before, you can only generate JavaScript using PHP, so if you want data from a PHP array in a JavaScript array, then you should just generate the JavaScript array using PHP – gitaarik May 03 '13 at 10:14
  • sorry i was actually actually gonna said ajax, not java hahahaha :D – R_A_P May 06 '13 at 04:13