tags:

views:

36

answers:

3

Hello,

I have two images, that I am using their value to insert into a db table. Do I Require a form or can I do without one.

<form name="form1" id="form1" method="post" action="save.php"><input type="image" src="images/test.png" border="0" id="star_image" value="<? echo $some_value; ?>" /></form>

<form name="form2" id="form2" method="post" action="save.php"><input type="image" src="images/test1.png" border="0" id="star_image1" value="<? echo $some_value1; ?>1" /></form

Would the above trigger the below jQuery code

$(document).ready(function(){

$('#form1').ajaxForm({});
$('#form2').ajaxForm({});

});

-----------------------------save.php--

insert into table (field) values('value')

.
.
.
.

And I want to submit the form using jQuery only

Thanks Jean

+1  A: 

You either need a form or you need to fake it with JavaScript.

Faking it with JavaScript is more effort and violates rule 2, so effectively, yes, you need a form.

That said, input type="image" creates a server side image map. It is designed to submit co-ordinates. Don't trust that its value will be submitted. If you really want to use an image input as a regular submit button, then check that its name.x is submitted (and this requires giving them unique names).

hmm, I see you don't have names at all at present. You must have a name before an input can be a successful control.

Finally, for input type="image" the alt attribute is mandatory (although not expressed in a machine readable way in the DTD so a validator won't pick it up).

David Dorward
+1|Nice link :D
aefxx
Assuming I give names then the code would it be like<script>$(document).ready(function() {$('#image1').ajaxForm(function(){});});Once the above jQuery is executed what happens? or is the above code wrong?
Jean
@Jean — an entire answer about why faking it with JavaScript is a bad idea and describing how to do it properly; and you ask for help with JavaScript libraries?! (That code would just error though, there is no element with that id and jQuery doesn't appear to have an ajaxForm method lurking in its API).
David Dorward
I am going to add some more code, please check the question again, in about a minute.
Jean
A: 

If you use jquery (and ajax), you don't need any form, nor values for controls, just send the parameters to your server-side script. But if you are using form to be submitted, then no, you can not submit images, common practice is to use hidden input fields instead.

alemjerus
A: 

If you rely on javascript, then NO, you don't need a <form>, since you'll probably bind the element to post a request with XHR, but for accessibility purposes, you should wrap it with a <form> element, to get it to work without javascript.

By the way, don't use a form is NOT recommended by the W3C, and your page will not validate for the reason explained above.

Boris Guéry
There is nothing invalid about having inputs outside a form. In most cases there is a whole lot of stupid, but it isn't forbidden by the DTD.
David Dorward
Infact, you have a nested form, done the, nested form too, works like a charm
Jean
No. A nested form IS invalid and the behavior for what happens with them is undefined, so using a nested form is doubly stupid.
David Dorward
the idea is to get the work done, no it is not a bad idea, but its possiblei cant seem to find the link
Jean