doutong7216 2013-05-03 03:59
浏览 25
已采纳

在JavaScript中的PHP反击不起作用

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

  • 写回答

3条回答 默认 最新

  • dougou1127 2013-05-03 04:13
    关注

    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!", ""];
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)