i have this piece of code i would like to validate using javascript. I have the validation code already , all i would like to know is how do you send a changeable variable to javascript i.e

while($row1 = mysql_fetch_array($result)){
     	$sel = ($row1['productid'] == $row['productid']) ? "selected=\"selected\"" : "";
         printf("<option value=\"%s\" %s>%s</option>\n", $row1['productid'], $sel, $row1['product']);
    	echo "</select></td>";
//resets counter to 0 on products list
	//print "&nbsp;";
     	print"<td><input style=\"text-align:center\" type=text name=productitemsupdate_$num value=\"$row[productitems]\" size=5 id=productitemsupdate_$num></td>";
  //	print "&nbsp;";   
	print"<td><input style=\"text-align:center\" type=text name=productcostupdate_$num value=\"$row[productcost]\" size=5 ></td>";
	$pay= $row[productitems] * $row[productcost];

The above code is part of a while loop. you will notice i have an id fields in the productsitems input. at the end to make it different from the other productsitems i have added a _$num at the end.
It is this num i would like to pass to javascript so it knows which line it is working on .

function validate(form) {
var phoneno = form.phoneno.value;
var deldate = form.deldate.value;
var name = form.name.value;
var extracharge = form.extracharge.value;
var productsitems = form.productitems_$num.value; <---- this line here i need help with

so in theory i need to validate if product items in all input boxes are integer.
i have the integer part i just know how to tell javascript which line it is working on.

Edited by kardklub: n/a

6 Years
Discussion Span
Last Post by kardklub

the input box count is not constant? I think you don't need to save its value to the some variable. Just use a loop and go trough all input boxes and check if they hold are integer values.


how would you do a loop in this case as the number of input boxes can change per order? im a bit of a noob at java? do you have an example ;)



I am not used to this, but I would take a look there:


Your input boxes should be probably in some let's say div container, and you loop throught them - for each of them get value, and if at least one of those value is not integer, you remember it. And you might probably even break the loop then.

EDIT: this should be possible without jQUery but I mostly do everything with jQuery as I think this should be simpler.

Edited by McLaren: n/a


lost? how do i set the id of the input fields of the form etc?


how do i set the id of the input fields

this is simple or I don't understood what you are asking :)

Lets say you want to print input field with id, you do it with php when generate the page:

echo '<input id = "your_id" type="text" name="firstname" />';



thanks for your response.
Sorry if i wasnt clear. as my product items are produced during a while loop. the id will change i.e

echo '<input id = "your_id_$id" type="text" name="firstname" />';

so row one's id will your_id_0
row twos will be your_id_1
im still struggling on how do i pass the individual id's to java. do i have to create a php array of the id's and send them to javascript . and then loop them
i hope this helps you understand my problem


If i have only one product on my form and i set the id to just id=prod it works . my problem is setting the id to be a different number using prod$id on the end and being able to pass the variable $id to javscript so it knows which input box has the invalid integer

Edited by kardklub: n/a


if you know how to select those elements with loop, after selecting it is easy to get ids:

var currentId = $('#element').attr('id');

but this example is not very good as we already have to know the id - id value is now 'element'. But we have somehow select with a loop so you will have something like this:

id = selectedElement.attr('id');

But I am not very familiar in selecting elemenst using loop, so you will have to find examples or somebody else will tell

EDIT: here I found examples: http://stackoverflow.com/questions/170180/looping-over-elements-in-jquery

Edited by McLaren: n/a


so does selectedElement.attr('id') pick up the variable assigned to the id. i.e
id=prod_$id (does it pick up the $id) of the selected id


id did a test i.e

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

in javascript i call

var pro = form.pro0.value;
   if(pro == "") {
    inlineMsg('pro','You can\'t leave this blank.',10);
    return false;

it doesn't pick up the php $num for somereason. even if i added it in manually like above

i can only assume that because the php is looped, at the end of the loop the id's ($nums) of the inputs are forgotten.

Edited by kardklub: n/a


an update i use this

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

and in java i have used as long as i right my variable pro0 or pro1 it works

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

if(pro.value == "") {
 		 inlineMsg('pro0','This cannot Be blank0',10);
 		  return false;
 if(pro1.value == "") {
 		 inlineMsg('pro1','Ythis cannot be blank1',10);
 		  return false;

this does work but anyhelp on passing the actual number with out me having to put it in manually would be great


ok, I have made a script with similar to yout one which cheks if its empty:

	$num = 0;
	echo "<input style=\"text-align:center\" type=text name=productitemsupdate_$num value=asd size=5 id=pro$num>";
	echo "<input style=\"text-align:center\" type=submit>";
$('form').submit(function() {
		var pro = $('#pro0').val();
		   if(pro == "") {
			   alert('You can\'t leave this blank.');
		    return false;
		  return false;

make sure you inlcude jquery library of course.


yes mate as soon as its complete ill post it here. just got one bug to solve ;)


that does the business ;)

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)
    inlineMsg('pro' + i,'You can\'t leave items blank.',10);
    return false;}
for(var i = 0; i < 2; i++)

oh, so it was constant number of fields, number can be 0 or 1 so you could even live without loop :)


It still is a dynamic field but i used a constant on the example to make sure the validation worked ;)

thanks for your input


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.