1

I have created this script to explode numbers from one data-get via php:

var n_polls=<?php echo $t_random_number;?>;
var myArray=n_polls.split(','); //explode

for (i=0;i<4;i++)
{
$("#t_sl_poll_"+myArray[i).hide();
}

The idea is to give some numbers from php for a random poll system, and I want explode this for close all in loop by the id. The problem is, I see something fail into the explode function for javascript, all time giving me nothing. How can I fix this?

Thank you.

7
  • 2
    You should look at the console for the syntax errors. That [i) isn't pretty sight. Commented Jul 11, 2013 at 14:53
  • My dude if this it´s writte right $("#t_sl_poll_"+myArray[i).hide(); Commented Jul 11, 2013 at 14:54
  • 1
    if $t_random_number is an array then you should echo json_encode($t_random_number); Commented Jul 11, 2013 at 14:54
  • in var n_polls only show number in this format 1,2,3,4,5 Commented Jul 11, 2013 at 14:54
  • @user2536883 then you should surround the echo "[".$t_random_number."]" Commented Jul 11, 2013 at 14:56

4 Answers 4

2

Why explode in Javascript when you could have PHP just insert an array?

<?php

$numbers = array(1,2,3,4);
?>

<script type="text/javascript">

var n_polls = <?php echo json_encode($numbers); ?>;

for (i in n_polls) {
   $("#t_sl_poll_" + n_polls[i]).hide();
}

There's further optimizations that could be done, but this'd be one place to start.

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at your source. Your error console would tell you the same thing. You're not putting your array (not a number) in quotes, so it fails to compile during runtime:

var n_polls="<?php echo $t_random_number;?>";
//QUOTES!   ^                              ^

var myArray=n_polls.split(','); //explode

for (i=0;i<4;i++)
{
    $("#t_sl_poll_"+myArray[i]).hide(); //Missing bracket
    //                       ^
}

Now it'd compile to

var n_polls="1, 2, 3, 4";

instead of

var n_polls=1, 2, 3, 4; //Useless non-working code - not enclosed in anything

3 Comments

actually this is wrong... it isn't an array as I originally thought it is a string so it should be surrounded by quotes or json_encoded
most iportantly, don't explode the array!
@Orangepill I know - annoyingly enough I had it written out as a string, then saw your comment and was like "damn - he's right!" and changed it. I've changed it back now. :-)
0

There are two things wrong here.

First: since you apparently hand over a string and not a number from php to javascript you have to write: var n_polls="<?php echo $t_random_number;?>";

Second: as pointed out in the comments already, you have to write $("#t_sl_poll_"+myArray[i]).hide(); to acutally address and element of your array.

Comments

0

your code should work as

var myArray= [<?php echo $t_random_number;?>];

for (i=0;i<4;i++)
{
    $("#t_sl_poll_"+myArray[i]).hide();
}

2 Comments

Don't use new Array(content)!
@JanDvorak Thanks for keeping me straight

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.