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开发者_Python百科\" 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;}
}
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.
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;
}
}
What ever your PHP (not my expertise), here is an example looping through: http://jsfiddle.net/UpG57/
精彩评论