SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    153
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    refreshing a div tag to change results

    Hello,
    I found a script that refreshes a div tag but it only works half way. I have a php if statement in the middle of the script and based on the if decision it should display the appropriate information.
    if no click display " button not pressed"
    else
    echo random number

    the first part works but not the sec. part.
    any ideas as to how i can fix this.?

    HTML Code:
    <html>
    	<head>
    		<meta charset="ISO-8859-1">
    		<script src="http://code.jquery.com/jquery-latest.js"></script>
    		<script>
    		$(document).ready(function() {
    			$('#btn_click').on('click', function() {
    				var url = 'refreshDiv.php';
    				$('#div1-wrapper').load(url + ' #div1');
    			});
    		});
    		</script>
    	</head>
    <body>
    <?php echo rand(10,1000); // test outside the div container?>
    	
    	<div id="div1-wrapper">
    		<div id="div1"  style="border:solid 2px red; width: 200px;">
    			<form name="form1" method="POST">
    				<?php 
    					if(empty($_POST["btn_click"]))
    					{ 	echo 'button not pressed'; }
    					else
    					 {	echo rand(10,1000); } 			
    				?>				
    		</div> 
    	</div>	
    	 <button type="button"  id="btn_click" /> click me! </button> 			
    	</form>	
    	</body> 
    </html>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Currently you are checking if the form has been posted:

    Code php:
    if(empty($_POST["btn_click"]))

    But instead of posting the form, you are loading a new page.

    Code javascript:
    $('#div1-wrapper').load(url + ' #div1');

    So don't do that. Get rid of all the scripting - you don't need it for what you're doing here.

    Instead of scripting, give the button a name and a value so that the empty() check will find something, and things will work as desired.

    HTML Code:
    <button type="submit"  name="btn_click" value="submitted" /> click me! </button>           
    Alternatively, you can get away with no value by using another technique to check if btn_click exists, by using filter_has_var, that lets you easily check for such things:

    Code php:
    if(!filter_has_var(INPUT_POST, "btn_click"))

    HTML Code:
    <button type="submit"  name="btn_click" /> click me! </button>           
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •