run javascript in ajax response

ganjasensation0098

New Member
Messages
36
Reaction score
0
Points
0
Hi.

My problem is that the Autofill class in php is not running on ajax response. how can I resolved this problem? Thanks in advance.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
I don't know where to start.

Really.

You want help but offer no real information.

No URL. No code. Nothing.

Just, "X doesn't work".
 

ganjasensation0098

New Member
Messages
36
Reaction score
0
Points
0
JS
PHP:
function getHighSchoolDivisionToArray(region)
{
    $.get(
        "../AMO/ajax_amo.php",
        "region=" + region + "&do_action=retrieve_highschool_division_to_array",
        function(data) {
            $("#txtRetrieveHighSchoolDivisionToArray").html(eval(data));            
        }
    );        
}

Form
PHP:
include_once "../classes/highschool.class.php";
include_once "../classes/autofill.class.php";
$school_division_arr_ = $oHighSchool->RetrieveHighSchoolDivisionToArray();
$oListHighSchoolDivision = new AutoFill("division");
for ($i=0; $i<count($school_division_arr_); $i++) {
   $oListHighSchoolDivision->addOption($school_division_arr_[$i]);
}
$oListHighSchoolDivision->setLimit(10);
$oListHighSchoolDivision->submitOnFill(false);
$oListHighSchoolDivision->create();

echo "<table>";
   echo "<TR>";
   echo "<TD>
            <div>
                <input type='text' name='school_region' id='school_region' onkeypress='return ValidKey(event, letters)' value='$school_region' title='Enter the region of the highschool' size='30' maxlength='50' onchange='getHighSchoolDivisionToArray(this.value);' onblur='getHighSchoolDivisionToArray(this.value);' />
            </div>
         <BR><center>Region</center></TD>";
   echo "<TD>			
            <div id='txtRetrieveHighSchoolDivisionToArray'>
            	<input type='text' name='division' id='division' onkeypress='return ValidKey(event, letters)' value='$division' title='' size='30' maxlength='50' />								
            </div>            				
         <BR><center>Division</center></TD>";
echo "</table>";

AJAX
PHP:
$school_region = "";
        if(isset($_GET["region"])){
            $school_region = $_GET["region"];
        }

        include_once "../classes/highschool.class.php";
        include_once "../classes/autofill.class.php";
        $oHighSchool = new HighSchool($cfg_db_type, $cfg_db_link, $cfg_db_name, $cfg_schema);
        $school_division_arr_ = $oHighSchool->RetrieveHighSchoolDivisionToArray($school_region);

        $oListHighSchoolDivision = new AutoFill("ajax_division");
        for ($i=0; $i<count($school_division_arr_); $i++) {            
            $oListHighSchoolDivision->addOption($school_division_arr_[$i]);            			
        }
        $oListHighSchoolDivision->setLimit(10);
        $oListHighSchoolDivision->submitOnFill(false);
		$oListHighSchoolDivision->create();        
		
        echo "<input type='text' name='division' id='ajax_division' onkeypress='return ValidKey(event, letters)' value='' size='30' maxlength='50' title='' />"; // title='Enter the division of the highschool'
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
What, exactly, do you expect the code to do? What does it do instead (including any errors)? The "Form" and "AJAX" labels aren't particularly helpful. Instead, state what files (e.g. "ajax_amo.php") the code fragments from, and include the functions they are a part of within the code itself.

There's both too much extraneous code and not enough context to debug it. You should write a minimal test case and post that.

PHP:
function getHighSchoolDivisionToArray(region)
{
    $.get(
        "../AMO/ajax_amo.php",
        "region=" + region + "&do_action=retrieve_highschool_division_to_array",
        function(data) {
            $("#txtRetrieveHighSchoolDivisionToArray").html(eval(data));            
        }
    );
}
If you're using eval, you're most likely doing something wrong (though not always). Does ajax_amo.php return JSON? If so, use a JSON parser (jQuery has one built in; simply specify "json" as the datatype to any AJAX function, or use getJSON). Does ajax_amo.php return javascript? If so, then you should use a datatype of "script" for the jQuery AJAX functions (or use getScript). If ajax_amo.php returns neither, then data isn't javascript, so eval(data) will fail. If ajax_amo.php returns HTML that includes <script> elements, you can use a datatype of "html". Script tags are evaluated when inserted into the document (as when you use the html method).


Form
PHP:
echo "<table>";
   echo "<TR>";
...
Ug. Table abuse. Keep your HTML semantic, which also means replacing the <center> elements with <label>s, and using styling rather than the <br>s. You could probably get rid of the <div>s and style the <input>s with block display.

You forgot to close the <tr>, but removing the table will make this moot.

The default type for inputs is "text", so you don't need to specify that. Of course, it won't hurt to include the attribute.

Rather than echoing each line, switch out of PHP. It's often more readable.
HTML:
$oListHighSchoolDivision->create(); 
?>
        <div>
            <input name='school_region' id='school_region' onkeypress='return ValidKey(event, letters)' value='<?php echo $school_region ?>' title='Enter the region of the highschool' size='30' maxlength='50' onchange='getHighSchoolDivisionToArray(this.value);' onblur='getHighSchoolDivisionToArray(this.value);' />
        </div> 
        <label for="school_region">Region</label>

        <div id='txtRetrieveHighSchoolDivisionToArray'> 
            <input name='division' id='division' onkeypress='return ValidKey(event, letters)' value='<?php echo $division ?>' title='' size='30' maxlength='50' />                                
        </div>                             
        <label for="division">Division</label>
<?php
 
Last edited:

ganjasensation0098

New Member
Messages
36
Reaction score
0
Points
0
I expect the code to do is after the ajax response. The autofill (in php) must run. But it seems not working.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
How do you know you are getting anything back from the server?

How do you know it is what you want?
 

ganjasensation0098

New Member
Messages
36
Reaction score
0
Points
0
I use firebug to know if the server returns or not. I see that the code is executed correctly but I don't know why is that the Autofill (class in php) does not appear.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
What if it is returning a 500 error?
What if it is returning a PHP error report?
What if the code you are getting back does note run properly?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
There's still both too much extraneous information and not enough essential information to get a clear picture of how the code is supposed to work and what it's actually doing. Follow the directions in my sig, including reading links. Apply the advice to my previous post.
 

ganjasensation0098

New Member
Messages
36
Reaction score
0
Points
0
Guys, I do a quick solution to my problem. Instead of using the Autofill (class in php), I used jQuery Autocomplete to finish my work. But still, I want to find out how to run javascript in ajax response. Thank you guys for your comments and advices, especially misson.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
But still, I want to find out how to run javascript in ajax response.
<accent nationality="spanish">You keep using that phrase. I do not think it means what you think it means.</accent>

As I pointed out previously, jQuery will evaluate JS in responses for you. How you direct it to do so depends on whether the response is JS, JSON or HTML containing script tags. However, that shouldn't affect whether AutoFill (which runs server-side) works, unless AutoFill generates a script tag. Note I can't say for certain that AutoFill does or doesn't require you to evaluate any JS because you still haven't told us what you expect the code to do, nor shown how it does it, nor have you told us which script is ajax_amo.php, nor shown example output of ajax_amo.php. Have you read anything I linked to, including in my sig?
 
Top