dongyuedaochen8415 2013-01-10 10:53
浏览 49
已采纳

使用.change的PHP jQuery如何将两者结合起来?

I have this codes with select name=client

with the help of jQuery i want to populate my textbox and other field. this select helps me to change my client option.

so for example i click the select im going to choice the client B. after that it will populate the text box and other fields. with the information came from database.

is my code correct, i post question here because i dont know why is my code not working.

my jQuery Codes

<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function(){  

    jQuery('select[name=client]').change(function(){
        var com_code = jQuery(this).val();

        <?
        $getClient = "SELECT * FROM jon_com_for";
        $getClient .= "WHERE com_code=";
        $getClient .= "'com_code'";

        $setClient = SET_SQL( $getClient );
        ?>

        jQuery('input[name=com_code]').val( '<?=$setClient['com_code'];?>' );
        jQuery('input[name=set]').val( '1' );
        jQuery('input[name=company_name_for]').val( '<?=$setClient['company_name_for'];?>' );
        jQuery('input[name=street_name]').val( '<?=$setClient['street_name'];?>' );
        jQuery('input[name=post_code]').val( '<?=$setClient['post_code'];?>' );
        jQuery('input[name=city]').val( '<?=$setClient['city'];?>' );
        jQuery('input[name=country]').val( '<?=$setClient['country'];?>' );

    });
});

</script>

my html code this html was inside of a dialog.

<div id="dialog" title="Step 1">
    <form id="submit_steps" method="POST" action="new-template.php">
    <input type="hidden" name="user_code" value="<?=$_SESSION['user_code'];?>" />
    <input type="hidden" name="com_code" value="<?=comucode();?>" />
    <input type="hidden" name="footnote" value="1" />
    <input type="hidden" name="set" value="1" />

    <div style="margin:20px auto;">

    <label for="client">Selecteer klant : </label>

    <select name="client">
    <?php
    $list_of_client = mysql_query( "SELECT * FROM jon_com_for" ) or die ( mysql_error() );
    while( $row = mysql_fetch_array( $list_of_client ) ) {
        echo '<option value="'.$row['com_code'].'">'.$row['company_name_for'].'</option>';
    }
    ?>
    </select>
    </div>

    <table cellpadding="3" cellspacing="0" width="100%">
      <tbody>
        <tr>
          <td align="right" valign="middle">BEDRIJFSNAAM :</td>
          <td><input name="company_name_for" size="30" id="company_name_for" class="input" type="text" /></td>
        </tr>
        <tr>
          <td align="right" valign="middle">ONDERWERP VAN OFFERTE :</td>
          <td><input class="input" name="sub_quo" id="sub_quo" size="30" type="text" /></td>
        </tr>
        <tr>
          <td align="right" valign="middle">STRAAT :</td>
          <td><input class="input" name="street_name" id="street_name" size="30" type="text" /></td>
        </tr>
        <tr>
          <td align="right" valign="middle">Postcode :</td>
          <td><input class="input" name="post_code" id="post_code" size="30" type="text" /></td>
        </tr>
        <tr>
          <td align="right" valign="middle">STAD :</td>
          <td><input class="input" name="city" id="city" size="30" type="text" /></td>
        </tr>
        <tr>
          <td align="right" valign="middle">LAND :</td>
          <td><input class="input" name="country" id="country" size="30" type="text" /></td>
        </tr>
      </tbody>
    </table>
    <div align="center">
        <input type="submit" name="save_client_prof" value="Opsslaan" /> or <a href="?c=cprof" id="cancel">Annuleren</a></p>
    </div>
    </form>
</div>

is that correct i put the PHP inside jQuery?

im sorry codes updated also my dialog is not working anymore.

here are the code that i had changed thank you all...

<script type="text/javascript">
$(function(){   
    $('select[name=clientx]').change(function(e){
        $('<img src="images/ajax-loader.gif" id="loading" style="width:auto;" />').appendTo("#client_details");
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var com_code = $('select[name=clientx]').val();
        var dia = jQuery( 'input[name=dia]' ).val();

        $.ajax({
            type:"GET",
            url:"forms/company.php",
            data: {dia: dia, com_code: com_code},
            dataType: 'html',
            target: '#dialog',
            success: function(data){
                $("#client_details").find('img#loading').remove();
                $("#client_details").html(data);
            }
        })
    }); 

});
</script>
  • 写回答

1条回答 默认 最新

  • drf97973 2013-01-10 11:06
    关注

    Putting PHP code inside a jQuery change event does not mean that the PHP code is run when the change event happens.

    PHP code written this way runs only once, when the page is loaded. This means that it will never know what the com_code JS variable contains after the click.

    In order to execute PHP code whenever the JS change event happens, you need to use a technique called Ajax. Your javascript code needs to make a call to an entirely separate PHP program that will query the data and return the results back to the browser for the javascript code to work with.

    This technique is well established, and jQuery provides functions that make it very easy to use. Look up some jQuery Ajax examples, and follow them from there.

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog