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.

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

报告相同问题?

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow