SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member Juriy's Avatar
    Join Date
    Aug 2011
    Location
    Astana, Kazakhstan
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Problem with ajax call

    Guys plz need a help.

    I have index page

    PHP Code:
    <form id="form1" name="form1" method="get" action="show.php">
    <select name="cg" onchange="SearchC();" id="catG"  > 
                 <option value="1" >ВСЕ КАТЕГОРИИ</option>          
                <option value="2"<? if($pcat=="2")echo "selected='selected'"?>>subject</option>     
                <option value="3" <? if($pcat=="3")echo "selected='selected'"?>>faculty</option> 
                <option value="4" <? if($pcat=="3")echo "selected='selected'"?>>lecture</option>                     
    </select>    
    </form>
    then
    PHP Code:

    function SearchC() {
        var 
    category = $("#catG").val();    
        var 
    query "searchbox.php?ct="+category;        
        $.
    ajax({
            
    urlquery,
            
    success: function(datatextStatusXMLHttpRequest) {            
                $(
    "#searchcontents").html(data);
                
    SearchCCallBack(false);            
            }
        });

    and searchbox.php
    PHP Code:
    <?
    if (($_GET['ct'] == '1'))
    {
    ?>                

        
        <select name="subj" id="subj" class="textbox">
        <option value="1"> math </option>
            <option value="2"<? if WHAT TO WRITE HERE echo "selected='selected'";?>>phys</option>
        </select>    
    <?php
    ?>

    how i can pass certain options "SELECTED" to show.php from searchbox.php. how to display everything in show.php?

    values from form1 m passing like -> if (isset($_GET['cg'])){$pcat=$_GET['cg'];}

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Hi Juriy and welcome to the SitePoint forums, the first thing i would like to point out is not to use $_GET queries with Ajax unless you escape the incoming value as they are more prone to attack.

    From what i can gather from the code you have above is when you select an option from your cg select box that gets sent to your searchbox.php script which then generates another sub select box with more selections.

    You question regarding this would be what code would you need to write for the selected="selected" attribute, is that correct?

    If so then my simple answer would be at this point nothing, without having a value to match against the IF statement you just need the <option> element for now. When it comes time to selecting that value you can do this very easily using jQuery as it appears your using that framework.

    On a side note i can see improvements which will increase the performance of your onchange event, please see the updated code below...

    PHP Code:
    <?php

    $pcat 
    = (int) $pcat;

    ?>
    <form id="form1" name="form1" method="get" action="show.php">
        <select name="cg" id="catG">
            <option value="1" >??? ?????????</option>
            <option value="2"<?php if ($pcat == 2) echo ' selected="selected"'?>>subject</option>
            <option value="3"<?php if ($pcat == 3) echo ' selected="selected"'?>>faculty</option>
            <option value="4"<?php if ($pcat == 4) echo ' selected="selected"'?>>lecture</option>
        </select>
    </form>
    Code JavaScript:
    $('#catG').change(function() {
        $.ajax({
            success : function(data, textStatus, XMLHttpRequest) {
                $('#searchcontents').html(data);
                SearchCCallback(false);
            },
            url : 'searchbox.php?ct=' + $(this).val()
        });
    });

  3. #3
    SitePoint Member Juriy's Avatar
    Join Date
    Aug 2011
    Location
    Astana, Kazakhstan
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    Hi Juriy and welcome to the SitePoint forums, the first thing i would like to point out is not to use $_GET queries with Ajax unless you escape the incoming value as they are more prone to attack.

    From what i can gather from the code you have above is when you select an option from your cg select box that gets sent to your searchbox.php script which then generates another sub select box with more selections.

    You question regarding this would be what code would you need to write for the selected="selected" attribute, is that correct?

    If so then my simple answer would be at this point nothing, without having a value to match against the IF statement you just need the <option> element for now. When it comes time to selecting that value you can do this very easily using jQuery as it appears your using that framework.

    On a side note i can see improvements which will increase the performance of your onchange event, please see the updated code below...

    PHP Code:
    <?php

    $pcat 
    = (int) $pcat;

    ?>
    <form id="form1" name="form1" method="get" action="show.php">
        <select name="cg" id="catG">
            <option value="1" >??? ?????????</option>
            <option value="2"<?php if ($pcat == 2) echo ' selected="selected"'?>>subject</option>
            <option value="3"<?php if ($pcat == 3) echo ' selected="selected"'?>>faculty</option>
            <option value="4"<?php if ($pcat == 4) echo ' selected="selected"'?>>lecture</option>
        </select>
    </form>
    Code JavaScript:
    $('#catG').change(function() {
        $.ajax({
            success : function(data, textStatus, XMLHttpRequest) {
                $('#searchcontents').html(data);
                SearchCCallback(false);
            },
            url : 'searchbox.php?ct=' + $(this).val()
        });
    });
    thank you for your reply.
    then Im moving to show.php I would like to see everything that I selected in index.php. How to make option from searchbox.php be "selected" because all selections should already be done in index.php
    i need to show appropriate cg and subj in show.php

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You could use inline PHP declarations to store the values in your JS code, see the below example.

    Code JavaScript:
    <?php $someInteger = 2; ?>
     
    var myVar = <?php echo $someInteger; ?>;
     
    $('#catG').change(function() {
        $.ajax({
            success : function(data, textStatus, XMLHttpRequest) {
                $('#searchcontents')
                .html(data)
                .find('select')
                .val(myVar);
     
                SearchCCallback(false);
            }
        });
    });

  5. #5
    SitePoint Member Juriy's Avatar
    Join Date
    Aug 2011
    Location
    Astana, Kazakhstan
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SgtLegend, thanks. this line was very helpful -> var myVar = <?php echo $someInteger; ?>;

    task is done ))


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •