Results 1 to 3 of 3

Thread: Sending javascript variable to php page

  1. #1
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Sending javascript variable to php page

    I encountered a new problem which I can't resolve after looking at 100+ topics on the net.

    I want to send a variable to a php page and then open it.

    Any tips?

    javascsript

    Code:
    <script type="text/javascript">
        
    $(document).on("click",".namen", function(e) {
    
    var woon = $(this).text();
    
        $('#uitkomst').load('member.php');
    
    
       $.post('member.php', {variable: woon});
        
        
        
        e.preventDefault();
        
        
    });
    
    </script>
    member.php

    Code:
    <?php
    echo variable;
    ?>
    I work with a local server xampp, could there be a problem?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There are many ways to do this, here's one (sending/requesting page):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Sending/Requesting</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script>
    $(document).on('click', '.namen', function(e){
    	var woon = $(this).text();
    	$.ajax({
    		type: 'post',
    		url: 'member.php',
    		data: {variable: woon}
    	}).then(function(data){$('#uitkomst').html(data);});
    	e.preventDefault();
    });
    </script>
    </head>
    <body>
    <a href="#" class="namen">Namen</a><br>
    <a href="#" class="namen">Something Else</a>
    <div id="uitkomst"></div>
    </body>
    </html>
    And on member.php:

    PHP Code:
    <?php
    if(isset($_POST['variable'])){
        echo 
    $_POST['variable'];
    }
    ?>
    But, if that's all you want to do, there's no need for AJAX or for an external page.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Bicklo (12-09-2016)

  4. #3
    Join Date
    Nov 2016
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Thank you sir, you did it again.

    It looks like I was close a few times but not close enough.

    Thank you so much.

Similar Threads

  1. Sending Sms using Javascript
    By kadi in forum JavaScript
    Replies: 3
    Last Post: 01-11-2014, 05:43 AM
  2. Replies: 7
    Last Post: 06-13-2011, 07:18 AM
  3. sending two id's to next page
    By auriaks in forum PHP
    Replies: 2
    Last Post: 11-24-2009, 09:25 PM
  4. Passing JavaScript Variable to JSP Page
    By juliep in forum JavaScript
    Replies: 1
    Last Post: 11-16-2007, 04:36 PM
  5. Sending values to the next page...?
    By djr33 in forum PHP
    Replies: 0
    Last Post: 03-27-2006, 02:34 AM

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
  •