dongyuedaochen8415
dongyuedaochen8415
2013-01-10 10:53

使用.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 drf97973 8年前

    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.

    点赞 评论 复制链接分享