Welcome Guest, Not a member yet? Register   Sign In
Loading Pages with JQuery javascript (SOLVED)
#1

[eluser]Unknown[/eluser]
Hello All,

Its been 2 months since learning and using codeIgniter and I LOVE IT! For me, it's the best way to program web sites.

Anyways here's my question:

I have a <DIV id="content_panel"> that is populated through a function in my Controller (ajaxprofile) from a javascript call through a button click.

Javascript Function
Code:
function process() {
    var url='http://www.somesite.com/index.php/main/ajaxprofile';

    $.post(url, {},
        function(pdata){
            if (pdata.header=='success')
            {
                $("#content_panel").html(pdata.content);
            }
            else {
                alert ("failedn\n"+pdata);
            }
        },
        "json"
    );
    return false;
}


Controller Function
Code:
function ajaxprofile() {

     $msg['header'] = 'success';
     $msg['content'] = $this->load->view('main/profile_form', '', true);

     echo json_encode( $msg );
}

Profile_form.php
Code:
&lt;form id="profile_form" name="profile_form" autocomplete="off" method="get" action=""&gt;

&lt;input type="text" name="username" id="username" value="michael" size="20" maxlength="50"/&gt;
&lt;input type="password" name="password" id="password" value="" size="20" maxlength="15"/&gt;

&lt;input class="submit" id="my-button" type="submit" value="Update" style="float:right;"/&gt;
&lt;/form&gt;

But it doesn't work. The INPUT submit button isn't bound to the javascript. I think it's because the Javascript is loaded with the page VIEW and the HTML is loaded later when a user clicks a link.

Does anyone know how to work around this? Does this mean I have to somehow reload the javascript?
#2

[eluser]Unknown[/eluser]
Solved.

I included the JAVASCRIPT after the HTML FORM in the php script. So the javascript is loaded when the partial page VIEW is called by the Controller when a button is clicked.

profile_form.php
Code:
&lt;form id="profile_form" name="profile_form" autocomplete="off" method="get" action=""&gt;

&lt;input type="text" name="username" id="username" value="michael" size="20" maxlength="50"/&gt;
&lt;input type="password" name="password" id="password" value="" size="20" maxlength="15"/&gt;

&lt;input class="submit" id="my-button" type="submit" value="Update" style="float:right;"/&gt;
&lt;/form&gt;


... ADD THE JAVSCRIPT HERE ...

Hope this helps someone... eventhough it really isn't a codeIgnighter issue.
#3

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter community, and thanks for posting your solution!




Theme © iAndrew 2016 - Forum software by © MyBB