1

as before the below code is in a while loop . it out puts values id=pro0 then next loop through i get id=pro1 and if i have another record i get id=pro3

print "<td><input style=\"text-align:center\" type=\"text\" name=\"productitemsupdate_$num\" value=\"$row[productitems]\" size=\"5\" id=\"pro$num\"></td>";

the above code works with

var pro = document.getElementById('pro0');

what im after is a loop in java script which will take the last $num from the php from and use that for the amount of times to loop the javascript function.

something like this i think

var i=0;
while (i<=$num) {
    var pro[i] = document.getElementById('pro'[i]);
    if (pro[i].value == "") {
        inlineMsg('pro'[i],'This cannot Be blank'[i],10);
        return false;
    } else {
        i++;
    }
}

breaks when it is false when all fields are right it is true. if i get this working it will save me guessing how many variable to check.

i have tried this but i cant this working either

var i=0;
var pro[i] = document.getElementById('pro' + i);
for(var i = 0; i < <?php $num ?>; i++) {
    if(pro[i].value == "")
        alert("You entered: " + pro[i].value)
    return false;
}

this works but only for the first line. if the 2nd line is emtpy and the first line is not it does not produce the alert

var i=0;
for(var i = 0; i < 3; i++) {
var pro = document.getElementById('pro' + i);
if(pro.value == "")
alert("You entered: " + pro[i].value)


return false;
}

i also changed the $num variable to a static number for my test record to see if it was my variable not being passed and it still did't work for the 2nd record any ideas

thanks

steve

working solution thanks for your help

var i=0;
var pro = [];

for(var i = 0; i < 2; i++) {
pro[i] = document.getElementById('pro' + i);
if(pro[i].value == ""){
    alert("You entered: " + pro[i].value)
return false;}

}

5
  • 7
    @Belina: you're joking, right? Commented Mar 16, 2011 at 12:58
  • Are you really sure that your code does what you expect? I added formatting for your code (I only added whitespaces, not changed any other character) and I'm not sure about your return false at the end of first for loop run... Commented Mar 16, 2011 at 13:28
  • yes if the first result it gets to is blank i need it to exit . and then when it is filled in if the next row is empty then it exits there Commented Mar 16, 2011 at 13:59
  • See my edit. You're missing braces for the if block. Commented Mar 16, 2011 at 14:10
  • exactly, just worked that out before logging back in lol thanks matt Commented Mar 16, 2011 at 14:14

3 Answers 3

2

PHP is server-side. JavaScript is client-side. If you want to insert a PHP variable into JavaScript, you have to treat your JS like HTML and use PHP to create the script. Something like this:

<!-- ... -->
<script>
    var num = <?php echo $num ?>;
</script>
<!-- ... -->

Then you can use this value of num elsewhere, even in external scripts (as long as num is declared beforehand).


Edit

Your if statement in the for loop is missing braces. Try this:

var i=0,
    pro[i] = document.getElementById('pro' + i);

for(var i = 0; i < <?php echo $num ?>; i++) {
    if(pro[i].value == "")
    {
        alert("You entered: " + pro[i].value)
        return false;
    }
}

Make sure that this JS is actually being processed by PHP. The easiest way to do that is to put the code into an inline <script> tag.

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

2 Comments

Don't you have to echo the $num to print it to the HTML?
You're right, I was mixing up the shorthand for echo, ex. <?=$num?>
0

In javascript, you concatenate strings using +. Therefore, you need to write

pro[i] = document.getElementById('pro' + i);

For your loop, that'd look something like

for(var i = 0; i < <?php echo $num ?>; i++) {

    ...

}

Complete code:

var i=0;
var pro = [];
for(var i = 0; i < <?php echo $num ?>; i++) {
    pro[i] = document.getElementById('pro' + i);
    if(pro[i].value == "") {
        alert("You entered: " + pro[i].value)
        return false;
    }
}

If you don't need the actual array for anything, for later, you could simply do:

var i=0;
for(var i = 0; i < <?php echo $num ?>; i++) {
    var pro = document.getElementById('pro' + i);
    if(pro.value == "") {
        alert("You entered: " + pro.value)
        return false;
    }
}

8 Comments

ive updated my post with your code but i just cant get it to work
I believe the $num should be echoed to print to the output. <?php echo $num ?>
sorry, I shouldn't have left the var keyword there, in the first line of code. pro[i] assumes an array already exists, it doesn't declare a new variable. Also, that line of code should be inside your loop. The way it is right now, it will only be executed once, and always with i=0.
@Steve: It's been ages since I wrote any PHP. Isn't <?php $num ?> shorthand for <?php echo $num ?> ?
thanks guys ill give this another ago. this is fast reponse time. ;)
|
0

What ever your PHP (not my expertise), here is an example looping through: http://jsfiddle.net/UpG57/

1 Comment

Your demo has a minor type. Would you mind putting the code into your answer as well? I'm not sure if there are any guarantees about how long fiddles will stick around if you don't save them to an account.

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.