如何将值从Codeigniter的form_hidden传递给javascript?

时间:2014-11-22 04:04:15

标签: javascript php codeigniter validation

我想将view_hidden的值从视图传递给javascript函数。

查看:

<?php echo validation_errors(); ?>
<?php

  $mode                 = (isset($is_editing))? 'disabled' : 'disabled';
  $client_name          = (isset($is_editing))? $clientcontent->client_name : NULL;
  $process              = (isset($is_editing))? 'Edit' : 'Add';
  $form_action          = (isset($is_editing))? 'client/update_client/' . $id : 'client/create_client';

?>
<style type="text/css">
  .check_exists
  {
    margin-top: 4px;
    margin-left: 9px;
    position: absolute;
    width: 16px;
    height: 16px;
  }
</style>
<?php echo form_open($form_action, 'id="frm" class="form-horizontal" style="margin: 0;"'); ?>
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
    <h4 class="modal-title"><?php echo $process ?> Client</h4>
  </div>
  <div class="span6" style="padding-top:20px;padding-bottom:10px;">
    <!-- Project Content Fields -->
    <div class="control-group">
      <p class="control-label">Client Name*</p>
      <div class="controls">
        <?php echo form_input('client_name', $client_name, 'id="client_name" style="height: 30px;"'); ?><span id="client_verify" class="check_exists" ></span>
      </div>
    </div>

    <?php echo form_hidden('old_name',$client_name); ?>

  </div>
<?php echo form_close(); ?>

   <script type="text/javascript">
  $(document).ready(function()
  {
    check_client("#client_name", "<?php echo base_url(); ?>client/check_availability", "#client_verify", "#old_name");
  })
// </script>

Javascript文件:

function check_client( id, url, spanid, old)
{

  var oldname = $(old).val();

  $('body').on('keyup', id, function(){
    if($(id).val().length >= 3)
    {
      var newname = $(id).val();
      console.info("The new name is " + newname + ":old name is" + oldname);

      $.ajax({
        url     : url,
        data    : {client_name : $(id).val()},
        type    : 'POST',
        success : function(data){
            if( data.is_available == 0 ){ // Here
                      //start fading the messagebox
                      $(spanid).css({ "background-image": "url('http://localhost/mapmces_3.4.2/assets/img/no.png')" });
                      $(spanid).attr( "title", "Already Exists" );
                      available = false;

                  }else if( data.is_available ){ // Here the json response
                     //start fading the messagebox

                     $(spanid).css({ "background-image": "url('http://localhost/mapmces_3.4.2/assets/img/yes.png')" });
                     $(spanid).attr( "title", "Available" );
                     available = true;

                   }else{
                    alert("Some error occured! Check Console");
                    console.error(data.reason);
                  }
                }
              });
    }
    else
    {
      $(spanid).css({ "background-image": "none" });
    }
  });
}

一切正常。萤火虫没有显示任何错误。该函数也有效,但我想添加另一个验证,检查oldname = newname。该函数能够获取newname的值,但无法获取来自form_hidden的oldname值。

请告诉我我做错了什么。非常感谢!

2 个答案:

答案 0 :(得分:0)

我要做的是加载脚本。之后,在你的html中,创建一些脚本标签并将你的表单隐藏到变量中。

离。

    <script src='main.js'></script>
    <script>
      var hello=<?php echo form_Hidden; ?>;
      thisIsAFunction(hello);
    </script>
  </body>
</html>

答案 1 :(得分:0)

刚刚改变

<script type="text/javascript">
  $(document).ready(function()
  {
    check_client("#client_name", "<?php echo base_url(); ?>client/check_availability", "#client_verify", "#old_name");
  })
</script>

<script type="text/javascript">
var old_name = "<?php echo $client_name; ?>";

$(document).ready(function()
{
  check_client("#client_name", "<?php echo base_url(); ?>client/check_availability", "#client_verify",old_name);
})
</script>