0

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
mysql_data_seek($result,0);
	//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];
	$id2=$num;

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.
kardklub

Edited by kardklub: n/a

3
Contributors
20
Replies
21
Views
6 Years
Discussion Span
Last Post by kardklub
0

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.

0

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 ;)

kardklub

0

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

http://api.jquery.com/each/

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

0

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

0

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" />';

:)

0

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

kardmagic

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

0

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

0

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

0

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

0

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

0

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

<div>
	<form>
	<?php
	$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>
</div>
$('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.

0

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

0

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;}
    
}
0
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 :)

0

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

thanks for your input

kardklub

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.