Skip to content
Advertisement

How to pass variable via AJAX / jQuery?

I would like to know how do I pass a variable to another php-file via AJAX?

The variable that has to be passed is called:

$id

The problem is that my form in ‘pm_form.php’ has no access to the variable ‘$id’.

My script looks like this (this script fires a modal-window and that modal window loads a form called ‘pm_form.php’):

$(".pm_link").colorbox($.extend(defaults, {
        initialWidth:'348',
        initialHeight:'348',
        innerWidth:'348',
        innerHeight:'348',
        type: "POST",
        href: "<?php echo $setting['site_url'];?>/includes/forms/pm_form.php",
    
        onComplete: function(){
            $("#cboxLoadedContent").appendTo("#cboxContent");
                            
            var title = 'Send Message';
            $('#cboxTitle').text(title);
        }
    }));

When I click on a submit button of my form the variable ‘$id’ shall be passed to the appropriate php-file.

This is my form:

<div id="pm_content" class="modal_font_indent">
<div id="pm_form">
    <form name="form1" method="post" action="<?php echo $setting['site_url']?>/index.php?id=<?php echo $id;?>&task=send_message&done=1">
    
        <div id="pm_subject" class="form_alt_design">
            <div id="pm_subject_txt"><label for="message_title">Subject:</label></div>
            <input type="text" name="message_title" id="message_title" class="pm_subject_textbox" value="" />
        </div>
    
        <div id="pm_message" class="form_alt_design">
            <div id="pm_message_txt"><label for="message">Message:</label></div>
            <textarea name="message" cols="50" rows="4" id="message" class="pm_message_textbox"></textarea>
            <div id="pm_chars_left">Characters left:</div>
        </div>    
        
        <div id="pm_submit">
            <input type="submit" name="Submit" value="" class="pm_button" />
        </div>
        
    </form>
</div>
</div>

I don’t know much about jQuery / AJAX.

Solution

I just echo my $id:

href: “/includes/forms/pm_form.php?id=”, and in my pm_form.php, I can grab that id parameter using the $_GET global as:

$id = $_GET['id'];

Advertisement

Answer

To pass variable from one PHP page to another, you need to use POST or GET method. Use POST[‘name of tag’] and store it in a separate variable.

Eg. : $variable1 = $_POST[name];

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement