www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Passing concatenated variables via get and javascript

  1. #1
    Join Date
    Apr 2012
    Posts
    21

    resolved [RESOLVED] Passing concatenated variables via get and javascript

    I have a concatenated list of posts like this:

    Code:
    <?php
    // this generates a list of divs with unique id's as $post_id's
    $my_posts .= '
    	<div class="storypost' . $post_id . '">
    		<a href="mystorypost.php?id=' . $post_id . '>' . $member_name . ' Post</a>
    		<a href="#" onclick="DoAction(' . $mem_id . ', ' . $post_id . ');">Remove?</a>
    	</div>
    ';
    ?>
    And here is the javascript/ajax, which sends the variables to the _removepost.php, in order to run that code, and delete the post from the database...

    Code:
    <script>
    function DoAction(mem_id, post_id) {
    	var r = confirm('Are you sure you want to remove this post?');
    	if (r == true) {
    		$.ajax({
    			type: "GET",
    			url: "_removepost.php",
    			data: "mem_id=" + mem_id + "&post_id=" + post_id,
    		});
    		$(".storypost"+post_id).hide();
    		return false;
    	} else {
    		return;	
    	}
    }
    </script>
    So, the thing actually works (the post gets deleted from the database). But what does not work, is:

    1) the div is not getting hidden via the .hide()
    2) after I click the onClick link, the page scrolls back to the top of the page... and I have to end up reloading the page to see the change.

    I believe there is something wrong with where I placed the "return false;" and something wrong with how I call the variable in .hide().

  2. #2
    Join Date
    Nov 2010
    Posts
    1,031
    hello again juniper.

    I'm not sure, but there are a couple of things you can try:

    1. I would put the return false directly in the onclick:
    Code:
    <a href="#" onclick="DoAction(' . $mem_id . ', ' . $post_id . ');return false">Remove?</a>
    2. I doubt this is the problem, but you have a dangling comma here after post_id:
    Code:
    $.ajax({
    			type: "GET",
    			url: "_removepost.php",
    			data: "mem_id=" + mem_id + "&post_id=" + post_id,
    		});
    some browsers (hello IE!) read that as meaning the object has more properties, then get confused when there are not.

    So, maybe if you fix those two things your other problem will magically disappear (I doubt it, but it's worth a try).

    Not that it will make any difference, but if all the classes you are creating are unique, you could just be using id's... not that it makes much difference, just that id's have to be unique whereas classes can be shared (or unique):

    Code:
    <div id="storypost' . $post_id . '">
    
    $("#storypost"+post_id).hide();

  3. #3
    Join Date
    Apr 2012
    Posts
    21
    Thanks, You are awesome... Worked like a charm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles