dongxia2030
2013-08-21 06:14 阅读 46

在jquery中单击复选框时如何将会话值获取到文本框

I'm having two pages with similar textboxes when user inserts data into first page and goes to next page, if he need to give same data am adding a checkbox, when user clicks it same data which is in session from before page has to be get into the second page variables through ajax. can someone help me please. thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dpbfb7119 dpbfb7119 2013-08-21 06:27

    Response for the Comment

    I made sample code which will give you idea about how to can do this.

    jQuery Code for checkbox change event

    $(function(){
    
        $('input:checkbox').change(function(){
    
            if($(this).is(':checked'))
            {
                $.ajax({
                    url : 'script.php',
                    success : function(session)
                    {
                        $('input:text').val(session);
                    }
                });
            }
    
        });
    
    });
    

    HTML

    <input type="text" />
    <input type="checkbox" />
    

    script.php

    <?php
    session_start();
    
    echo $_SESSION['name_of_the_session_variable'];
    exit;
    ?>
    

    EDIT

    $("#checked").click(function()
    {
        if ($(this).is(':checked'))
        {   
            $('#provisional_total_public_funding').val(<?php echo empty($this->session->store['actual_info']['actual_total_public_funding']) ? '' : $this->session->store['actual_info']['actual_total_public_funding']; ?>);
        }
    });
    

    Ajax Request Response

    <select name="fin_year" id="fin_year">
        <option value="" >Please select an year</option>
        <option value="<?= $actFinYr; ?>"><?= $actFinYr; ?></option>
    </select>
    
    <script type="text/javascript">
    
        $(function(){
    
            $('#fin_year').change(function()
            {
                var options = $(this);
                if(options.val() != '')
                {
                    $.ajax(
                    {
                        url     : 'CODEIGNITER_HTTP_URL/'+options.val(),
                        beforeSend : function()
                        {
                            //show loading
                        },
                        success : function(response)
                        {
                            //play with the response from server.
                        }
                    });
                }
            });
    
        });
    
    </script>
    
    点赞 评论 复制链接分享
  • dongxin5054 dongxin5054 2013-08-21 06:45

    I'd use jQuery like this:

    HTML 1st page:

    input1 <input type="text" id="input1" name="input1"/>
    input2 <input type="text" id="input2" name="input2"/>
    

    jQuery 1st page:

    $input1 = $("#input1");
    $input2 = $("#input2");
    $input1.keydown(function(){
        $.post("yourPHP.php", {input1: $input1.val()});
    });
    $input2.keydown(function(){
        $.post("yourPHP.php", {input1: $input1.val()});
    });
    

    PHP 1st page:

    if(session_id() == '') {
        session_start();    
    }
    if(isset($_POST['input1'])){
        $_SESSION['input1'] = $_POST['input1'];
    }
    if(isset($_POST['input2'])){
        $_SESSION['input2'] = $_POST['input2'];
    }
    

    HTML 2nd page:

    input1 <input type="text" id="input1" name="input1"/>
    input2 <input type="text" id="input2" name="input2"/>
    <br/>
    radio1 <input type="radio" id="radio1" name="radio"/>
    radio2 <input type="radio" id="radio2" name="radio"/>
    

    jQuery second page:

    $input1 = $("#input1");
    $input2 = $("#input2");
    $radio1 = $("#radio1");
    $radio2 = $("#radio2");
    $radio.click(function(){
        $.post("yourPHP.php", {request: "input1"}, function(data){
            $input1.val(data);
        });
    });
    $input2.keydown(function(){
        $.post("yourPHP.php", {request: "input2"}, function(data){
            $input2.val(data);
        });
    });
    

    PHP 2nd page:

    if(session_id() == '') {
        session_start();    
    }
    if(isset($_POST['request'])){
        switch($POST['request']){
            case 'input1':
                echo $_SESSION['input1'];
                break;  
            case 'input2':
                echo $_SESSION['input2'];
                break;
        }
    }
    

    I hope it works.

    点赞 评论 复制链接分享

相关推荐