Submit Image Form Validation - javascript

$('#submit-img').on('click', function() {
$('#img').attr('src', $('#url').val());
});
function validate() {
if ($('#img').attr('src') === '../static/img/placeholder.png'){
return false;
}
}
<form method="post" action="/{{ curr_user }}" enctype="multipart/form-data" onSubmit="return validate(this);" >
<div class="imagey">
<img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="image" />
</div>
<span class="input-url">
<input type="text" name = "url" id="url" placeholder="http://" maxlength="320"/>
</span>
<input type="submit" id="submit-img" name="op" value="Submit" class="form-submit"/>
</form>
This validation code does not work for me. Any guesses why?
Also, if replace the if statement in the validate function with just alert($('#img').attr('src'))
The alert says Undefined

There's mulitple errors in your code.
First of all this creates an endless loops of 404s in the browser
<img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="image" />
Second, it should be $('.submit-img'), not $('#submit-img'), since you've set the class attribute, not the id attribute.
Thirdly, you need to wrap jQuery code into $(document).ready(function() { ... });
After fixing that, the code works for me on jsfiddle
[Edit:]
Ok, probably this is what you are looking for:
$(document).ready(function() {
$('#url').on('keyup', function() {
$('#img').attr('src', $('#url').val());
});
});
When the users enters a URL, the image is shown.
Example on jsfiddle

Related

Trying to show an element on form submit

I'm trying to show a loader.gif image when my form is submited.
My loader.gif has display:none in CSS, and I want to display it, when the form is submited.
I'm trying to do this using my simple code below.
It seems correct for me but it's not working.
Do you see something wrong here?
jQuery:
$(function(){
$('form').submit(function(){
$('.loginbox h1 img').fadeIn('fast');
});
});
CSS:
.loginform h1 img {
float:right;
margin:7px 0;
display:none;
}
HTML:
<h1>Login Form: <img src="img/loader.gif"/></h1>
<form name="login" action="" method="post">
<label>
<span>User:</span>
<input type="text" name="user" />
</label>
<div class="label">
<span>Pass:</span>
<input type="password" name="pass" />
<input type="submit" value="Login"/>
</div>
</form>
I suppose that is because, once it is submitted, it is posting back or reloading or redirecting.
You can do this:
$(function(){
$('form').submit(function(e){
e.preventDefault(); // prevents submission
$('h1 img').fadeIn('fast'); // since you aren't using that class
$.ajax({ ... }); // use ajax to submit the form
});
});

prevent redirect to php file after form submit

After researching this for too many hours I finally decided to post the question myself, as what is working for others, doesn't seem to work for me. Please keep in mind that I am fairly new to ajax and jquery, but as I am on a deadline with my current project I wont have time to go through it all.
I have the following html form:
<div class="form">
<form id="savePlacemarkForm" method="post" action="createPlacemark.php">
<div>
<input type="text" id="placemarkName" name="placemarkName" placeholder="Name:"/>
</div>
<div>
<input type="text" id="placemarkAddress" name="placemarkAddress" placeholder="Adress:"/>
</div>
<div>
<input type="text" id="placemarkTag" name="placemarkTag" placeholder="Tags:"/>
</div>
<div>
<textarea id="placemarkDescription" name="placemarkDescription" placeholder="Description" rows="1" cols="1"></textarea>
</div>
<div>
<input id="latitude" type="text" name="latitude"/>
</div>
<div>
<input id="longtitude" type="text" name="longtitude"/>
</div>
<button class="md-close" id="savePlacemark" onclick="createPlacemark();"/>Save</button>
</form>
<button class="md-close">Cancel</button>
<script src="my_script.js" type="text/javascript"></script>
</div>
As you see I have the action set to createPlacemark.php which takes input from these fields and saves it to my DB, which works fine!! However since this should work without redirecting or resubmitting the page, meaning ajax! I include my_script.js which looks like this:
$("#savePlacemark").click( function() {
$.post( $("#savePlacemarkForm").attr("action"),
$("#savePlacemarkForm :input").serializeArray();
});
clearInput();
});
$("#savePlacemarkForm").submit( function() {
return false;
});
function clearInput() {
$("#savePlacemarkForm :input").each( function() {
$(this).val('');
});
}
As you see it does the post for me, which works, but for some reason the return false; doesnt seem to work for me, as I am continuously redirected to the before mentioned php file.
Any help would be greatly appreciated! thx!
Try the following. The .submit() function actually triggers a submit for the form. Take this out of the script since you don't need it when you've already posted the values with $.post.
$("#savePlacemark").click( function() {
$.post( $("#savePlacemarkForm").attr("action"),
$("#savePlacemarkForm :input").serializeArray();
});
clearInput();
});
function clearInput() {
$("#savePlacemarkForm :input").each( function() {
$(this).val('');
});
}
try this
<button onclick="return createPlacemark();">Save</button>
it may help you.

Accessing img src with javascript

function validate(form) {
while ($('#img').attr('src')=="../static/img/placeholder.png") {
return false;
}
return true;
}
^This code does not work for me. I want to have a form that does not submit while the placeholder img still exists. What mistake am I making with my code?
HTML:
<form method="post" action="/{{ curr_user }}" enctype="multipart/form-data" onSubmit="return validate(this);" >
<div class="submit-image">
<img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="submitted image" />
</div>
<span class="input-url text">
<input type="text" id="url" placeholder="http://" maxlength="320"/>
</span>
<div class="form-actions">
<input type="submit" id="submit" name="op" value="Submit" class="form-submit"/
</div>
</form>
From your current example, this should be enough:
function validate() {
return $('#img').attr('src') != '../static/img/placeholder.png';
}
$('#submit').on('click', function() {
return validate();
});
See a more detailed example here: http://jsfiddle.net/ENnLC/
Maybe use a Regex to confirm that src contains the placeholder.png
if( ( /placeholder\.png$/.test( $('#img').attr('src') ) ) {
return false;
}
Just one other point. Your code here
<img id="img" src="../static/img/placeholder.png" onerror="this.src = '../static/img/placeholder.png'" alt="submitted image" />
You are loading the same image on the onerror event which will attempt to load the same image and trigger the onerror event again and lead you to enter an infinite loop.

Appending to div and being unable to bind the added buttons

I have a php function that echos code of a form that has a button. when that button is click it will append a file field and a another button that if clicked would repeat the above. however when the added button is clicked nothing happened.
I am thinking it has something to do with binding the button.
Here is the code:
$formtoreturn = '
<div class="form_holder">
<form action="'.site_url().'xxxxx" method="post" enctype="multipart/form-data" >
<div id="fields_holder">
<span class="form_holder-label" id="fileC">Select image or images</span>
<div class="fields" id="field1">
<input type="file" name="fileID-input[]" id="fileID-input" class="fileC-input">
<a rel="add" id="add_file1" class="add_file" href="javascript:;"><img src="wp-content/plugins/wl_extendor_wishlist/images/add-icon.png"></a>
</div>
</div>
<div class="clear"></div>
<input type="hidden" value="'.$u_id.'" name="user_id" id="user_id">
<input type="submit" name="submittestimonial" value="Submit Testimonial">
</form>
<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</div>
<script>
var filecount = 1;
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j(\'#fields_holder\').on(\'click\', \'.add_file\', function() {
var toappend = \'<div class="fields" id="field1"><input type="file" name="fileID-input[]" id="fileID-input" class="fileC-input"><a rel="add" id="add_file1" class="add_file" href="javascript:;"><img src="wp-content/plugins/wl_extendor_wishlist/images/add-icon.png"></a></div>\';
if($j(this).attr("rel") == "add"){
$j(this).attr("rel", "ded");
$j("#fields_holder").append(toappend);
filecount++;
}
});
});
</script>
';
echo $formtoreturn;
Hope the code and my question is clear.
Thanks in advanced.
I would suggest you to change your JavaScript code from
$j('#fields_holder').on('click', '.add_file', function() {
// ...
});
to
$j(document).on('click', '#fields_holder .add_file', function() {
// ...
});
Then the event will bubble up and handled in click handler of a document.
UPDATE
Though I've made a test and it's working well for me in both variants.

Form submit problem using JavaScript in Internet Explorer 6

I have just done the code for submit the form using JavaScript.
It works in all browsers except in Internet Explorer 6.
I have pasted my HTML form and JavaScript code below.
Can you please find what's the problem with it?
JavaScript:
<script type="text/javascript" language="javascript">
function dodelete(image_id)
{
if (confirm("Are you sure want to delete this image?"))
{
document.getElementById('image_id').value=image_id;
document.del_form.submit();
}
}
</script>
HTML Code:
<form name="del_form" id="del_form" method="post">
<input type="hidden" name="do" id="do" value="delete" />
<input type="hidden" name="image_id" id="image_id" />
</form>
Function Call Code:::
<p class="video">
<a href="javascript:void(0)" onclick="dodelete('<?php echo $row['image_id']?>')">
<img src="<?php echo $cfg->admin_image_path; ?>/delete_icon1.gif" border="0" alt="Delete"/>
</a>
</p>
What is returned by:
document.getElementById('image_id')
It returns one INPUT element of collection of elements?
Try to replace:
document.getElementById('image_id').value=image_id;
with:
document.del_form.image_id.value=image_id;
OnSubmit call for javascript would help.
<form name="del_form" id="del_form" onsubmit="dodelete(value);" >
<input type="hidden" name="do" id="do" value="delete" />
<input type="hidden" name="image_id" id="image_id" />
</form>
There are two things I'd like to try, I'm not sure why or when this doesn't work it seems random, first try to set a timeout for the submit :
document.doSubmit = function() {
document.del_form.submit();
}
setTimeout("document.doSubmit();", 100);
Sometimes, just return something after the click works :
<input type="hidden" name="image_id" id="image_id" onclick="submitFormFunction(); return false;” />
What happens if you replace:
document.del_form.submit();
with:
document.getElementById('del_form').submit()
Try to move method='POST' to the beginning of form element definition.
I mean -- method attribute should be the first attribute of form element.
If I remember well, this fixed some problems with submitting forms on IE6.
I have edited your code a little bit.
<script type="text/javascript" language="javascript">
function dodelete(image_id)
{
if (confirm("Are you sure want to delete this image?"))
{
document.getElementById('image_id').value=image_id;
document.del_form.submit();
}
return false;
}
</script>
and the HTML with some test Image ID and it works in IE6
<form name="del_form" id="del_form" method="post" onSubmit="return(dodelete('2'));">
<input name="do" id="do" value="delete" />
<input name="image_id" id="image_id" />
<input type="submit" value="Submit">
</form>
Let's try again :).
What happen after replacing:
onclick="dodelete('<?php echo $row['image_id']?>')">
with:
onclick="dodelete('<?php echo $row['image_id']?>'); return false;">
What exactly doesn't work with your code? Throw JS error, there is no server request, values in request are empty?
And (maybe the most important question) -- where is action attribute for your del_form form?
replace
<a href="javascript:void(0)"
with
<a href="#"
IE have problem with that for the dom that fires the JS submit.

Categories

Resources