0

I have the following Code which works good now,

<!DOCTYPE html>
<html>
<body> 
    <div id="one">
    echo "data 1";
   <input type="hidden" id="one" name ="one" value="'.$one.'">

  </div>

  <div id="two">
     echo "data 1";
    <input type="hidden" id="two" name="two" value="'.$one.'">

  </div>
</body> 
</html>

for each DIV I have data 1 2 3...more comes from MySQL php query. So, when the Hidden Id = one delay some second and show the next result data 2, until we get the input hidden to one again. that all come from database. automatically. I have all that working.

I am able to use ajax to show the data, but when i come to read the Input value and change accordingly. I have problem...

I have tried..

var value1  = $('one').attr( "value" ); //
 var value2  = $('two').attr( "value" ); //
 (function data() {
                $.ajax({
                    url: 'data.php',
                    if (value1 == 'one') { 
                    success: function(data) {
                    $('#one').html(data);
                    },
                    }
                    complete: function() {
                        // Schedule the next request when the current one's complete
                        setTimeout(data, 5000);
                    }
                });
            })();

But it seems not working....

2
  • The first problem I see is that you've got the div and the inputs as having the same id. These need to be unique. Perhaps label each as div-one and input-one, then div-two and input-two. Commented Oct 17, 2013 at 10:04
  • Also, when calling the ID's in jQuery don't forget the # sign. $('#one') not $('one') Commented Oct 17, 2013 at 10:05

1 Answer 1

1

A couple of small modifications may help this work:

<!DOCTYPE html>
<html>
<body> 
    <div id="div-one">
    <?php echo "data 1"; ?>;
   <input type="hidden" id="one" name ="one" value="<?php $one; ?>;">

  </div>

  <div id="div-two">
     <?php echo "data 2"; ?>;
    <input type="hidden" id="two" name="two" value="<?php $two; ?>;">

  </div>
</body> 
</html>

Javascript:

var value1  = $('#one').val();
var value2  = $('#two').val();
 (function data() {
                $.ajax({
                    url: 'data.php',
                    success: function(data) {
                       if (value1 == 'one') { 
                          $('#div-one').html(data);
                       }
                    },
                    complete: function() {
                        // Schedule the next request when the current one's complete
                        setTimeout(data, 5000);
                    }
                });
            })();
Sign up to request clarification or add additional context in comments.

5 Comments

It seems doesn't load the next data, why?
Have you checked the result from the ajax request? Do a console.log(data); in the success to see what is being returned...
Ah, I just updated the javascript code to use .val() instead of attr. Give that a go.
I am getting the following error, SyntaxError: missing : after property id if (value1 == 'one') {
Ok. I think I got this. I've now placed the "if (value1 == 'one') {" part inside the success function. This makes far more sense, because success is a property of the ajax: function api.jquery.com/jQuery.ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.