Load external php file in jquery modal dialog onclick

 ̄綄美尐妖づ 提交于 2019-11-30 13:17:41

In your link

<a href="#" name="reg_link">Sign-Up!</a>

you have name="reg_link" that should be id="reg_link" instead, i.e.

<a href="#" id="reg_link">Sign-Up!</a>

So it'll work with following code

$('#reg_link').click(function(e) {
    e.preventDefault();
    $('#register').load('register.php');
});

To make it a dialog you can use

$(document).ready(function() { 

     var dlg=$('#register').dialog({
        title: 'Register for LifeStor',
        resizable: true,
        autoOpen:false,
        modal: true,
        hide: 'fade',
        width:350,
        height:275
     });


     $('#reg_link').click(function(e) {
         e.preventDefault();
         dlg.load('register.php', function(){
             dlg.dialog('open');
         });
      }); 
});

Just an example.

Create the dialog after you load the page .load() replaces the content of the container with the new content

Your click handler has syntax errors, It looks like your passing a combination of a function and an object as the argument, it should be a normal function. Like

$('selector').click (function() {
     //code
});

Also your <a> element has reg_link as a name not id

$(document).ready(function() { 
    $('#reg_link').click (function() {
        $('#register').load ('register.php', function(){
            $('#register').dialog({
                title: 'Register for LifeStor',
                resizable: true,
                modal: true,
                hide: 'fade',
                width:350,
                height:275,
            });//end dialog   
        });
    });
});

<td><font size="2">Not registered? <a href="#" name="reg_link" id="reg_link">Sign-Up!</a></td>

I'm not entirely familiar with the .dialog() function, but you're using .click() wrong. Part of the issue is some confusion regarding curly braces {}. They're used for two completely separate things, and you're mixing the two up here.

The first use of curly braces is to indicate the interior of a block: the inside of a loop, the inside of a condition, the inside of a function. For example:

// some code in the global scope
function something()
{
    // some different code within this function block
}
// function's done, we're back in global scope

The second use is JSON (JavaScript Object Notation) for an object or associative array, where properties or values are paired with names or keys in the following format:

var jsonSomething = {
    key1: value1,
    key2: value2,
    etc: etcvalue,
};

When you write $('#reg_link').click (function() {, you're opening up a function block with that curly brace, not starting a JSON. Thus, when you write open: (as if this were a JSON and you're setting the open key), something is definitely not going to work the way you expect it to (I'm surprised... kind of... that there's no error, actually). What you need to write within those curly braces is the code of a function. In this case, it's probably just this:

$('#reg_link').click (function() {
    $('#register').load ('register.php');
});

In general, jQuery uses both of these versions a lot, and often mixes them together (functions that accept JSONs as arguments, or JSONs that include function callbacks as entries), so it's really important to understand which is which.

EDIT: Some Googling re:.dialog() suggests that you'll also need to call it after .load(), which means that block should look something like this:

$('#reg_link').click (function() {
    $('#register').load ('register.php').dialog(/*argument(s) here*/);
});

Based on your own code, .dialog() actually is an example of a function that takes a JSON as an argument, so assuming that bit's correct, the full code is like this:

$('#reg_link').click (function() {
    $('#register').load ('register.php').dialog({
        title: 'Register for LifeStor',
        resizable: true,
        autoOpen: false,
        modal: true,
        hide: 'fade',
        width:350,
        height:275,
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!