I have this code :
<script type="text/javascript">
$(document).ready(function() {
$('a[name=linkArtist]').click(function(e) {
e.preventDefault();
$('#changeArtist').val($(this).attr('title'));
});
});
</script>
<form action="index.php?explore=browse" method="post">
<input type="hidden" id="changeArtist" name="artist" value="<?=$artist?>" />
<a name="linkArtist" title="a" href="#">A</a> -
<a name="linkArtist" title="b" href="#">B</a> -
<a name="linkArtist" title="c" href="#">C</a>
</form>
When I click on the button, I set the link value (as 'title') to the hidden field, and after I'd like to send that form! How can I do it? And what do you think about this code? Can I better it?
JavaScript:
<script type="text/javascript">
$(document).ready(function() {
$('a[name=linkArtist]').click(function(e) {
e.preventDefault();
$('#changeArtist').val($(this).attr('title'));
$('#myForm').submit();
});
});
</script>
The form:
<form action="index.php?explore=browse" method="post" id="myForm">
Note the form id and the change to the click handler to access it.
I don't think you need to use preventDefault() at all. The form should not submit until the function has returned.
Related
I want to send this query (EA452401760IN) to https://track.aftership.com/india-post/.
After a successful submission by Form the URL will be
https://track.aftership.com/india-post/EA452401760IN
Form
<form method="POST" action="https://track.aftership.com/india-post/" target="_blank"> <input type="text" value="EA452401760IN"></input><button type="submit"> submitc</button></form>
Do I need a PHP file for this?
If yes, how do I write a php file for this problem.
Pure Javascript approach with Jquery
<input type="text" id="trackingNumber" value="EA452401760IN"></input>
<button onclick="go()">Track</button>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script>
$( "button" ).on( "click", function( event ) {
window.location.href = "https://track.aftership.com/india-post/" + $('#trackingNumber').val();
});
</script>
JSBIN: https://jsbin.com/fuqovocoza/edit?html,console,output
<?php if(($_SERVER["REQUEST_METHOD"] == "POST")){
//Check that the form has been posted if it has been then redirect to the correct page
header ('Location: https://track.aftership.com/india-post/' . $_POST['query']);
}
else
{
if the form has not been submitted display the form
<form method="POST" action="https://track.aftership.com/india-post/" target="_blank"> <input type="text" name="query" value="EA452401760IN"></input><button type="submit"> submitc</button></form>
Note that I have changed the name of the input field to query.
Full page is at http://f14.co/auto-search/reno
I have the following checkbox set up outside a form:
<div class='span5' style='margin-left:0px !important;'>
<label for='model0'>
<input type="checkbox" name="model0x" id="model0x"
value="Accord" style='margin-top:-5px !important;'> Accord</label>
</div>
I have this javascript between the checkbox and the form:
<script>
if($("#model0x").is(':checked')){
$("#model0_is_checked").val($("#model0x").val());
}else{
$("#model0_is_checked").val("Not Checked");
}
</script>
Finally, I have this hidden input to call that value inside the form when the item is checked or not:
<form method="post" class="form-horizontal" id="final_form" action="send_mail.php">
<input type="hidden" id="model0_is_checked" name="model0_is_checked">
MORE FORM STUFF AND SUBMIT BUTTON
</form>
No matter what I'm getting no value in the send_mail.php ....what am I doing wrong?
$('#model0x').click(function(){
var self=this;
$("#model0_is_checked").val($(self).is(':checked')?self.value:'Not Checked');
// do something
});
// or use submit ()
Bind the function to the form submit.
<script>
$('#final_form').on('submit',function(){
if($("#model0x").is(':checked')){
$("#model0_is_checked").val($("#model0x").val()); }
else { $("#model0_is_checked").val("Not Checked");}
});
</script>
jsFiddle example http://jsfiddle.net/KZrLp/
Your Javascript runs once, when the page (or more accurately, the <script> tag) is loaded. You have to make it run when the form is submitted instead:
<script type="text/javascript">
function updateHidden() { // Find a better name ;)
if($("#model0x").is(':checked'))
$("#model0_is_checked").val($("#model0x").val());
else
$("#model0_is_checked").val("Not Checked");
}
$(document).ready(function() { $('#final_form').submit(updateHidden); });
</script>
P.S. : not tested code
I want to validate my form on button click.
For that I used ("#trade").validate();.
But My problem is that when user click on my button so it's call one more function which post my data.
<input type="button" class="button" value="Save" onClick="create_trade();">
I used
<script type="text/javascript">
$(document).ready(function(){
$("#trade").validate();
});
</script>
Now how will I validate this form.
After form validation, you call the function create_trade(), in the submitHandler event.
<script type="text/javascript">
$(document).ready(function(){
$("#trade").validate({
submitHandler: function(form) {
create_trade();
}
});
});
</script>
use form tag's onsubmit attribute.
like this <form onsubmit="return validate()"
This will not submit form data if the function returns true so make sure you return true only if your validation is done otherwise return false from the function so that your form data will not be submitted.
<form name="form1" method="post" action="">
<input type="button" class="button" name="save" value="Save">
</form>
<script>
function fn_submit()
{
document.form1.submit();
}
</script>
try this....
Try this code if you want to call the validation function on clicking the button
<script type="text/javascript">
$(document).ready(function(){
$("input[type=button]").click(function() {
//validation code
});
});
</script>
<?php
if (!empty($_FILES)){
echo "<pre>";
echo $_FILES['file']['type'];
echo "</pre>";
return;
}
?>
<script type="text/javascript">
function autoUpLoad(){
$("#txtHint").html("<center><img src='include/images/loader.gif' />Reading selected file...</center>");
$(document).ready(function(){
$("#file").change(function(){
$("#myForm").submit(function(data){
$("#txtHint").html(data);
});
});
});
}
</script>
<form id="myForm" method="post" enctype="multipart/form-data">
<input type="file" name="file" id = "file" onchange='autoUpLoad()'/>
<input type="submit" value="Send" />
</form>
<div id="txtHint">
test
</div>
The above code is not working and I am not sure what is wrong here? It works only if I remove these lines:
function(data){
$("#txtHint").html(data);
}
It just doesn't allow me to return data to txtHint. Can anyone explain to me how to make it work?
You are binding a submit event, not fire it.
.submit() method with a callback as parameter is used to bind submit event, without parameter to fire the submit event.
You should do something like below:
$(document).ready(function () {
$("#myForm").submit(function (data) {
$("#txtHint").html(data);
});
$("#file").change(function () {
$("#myForm").submit();
});
});
Hi i'm using php and jquery. I have create dinamically a list of a div like that
<div class="divclass" id="<?php echo $i-1;?>">
<a href=" <?php echo $this->url(array('controller'=>'controller name','action'=>'action name'));?>">
<span>Date: </span>
</a>
</div>
My javasctipt script is, i pick the name of the id clicked, i set the hidden parameter to the name of the id and i want to submit the form
<script type="text/javascript">
$(document).ready(function() {
$('.divclass').click(function(){
var idarray = $(this).attr("id");
document.getElementById('testo').value=idarray;
document.forms["prova"].submit();
});
});
The form is:
<form id="prova" method="post" action="<?php echo Zend_Controller_Front::getInstance()->getBaseUrl().'/controller-name/action-name';?>">
<input type="hidden" value="" id="testo">
</form>
</script>
But in the next page i don't have the post parameter.
You need to give name attribute to #testo and then try this:
e.g
<input type="hidden" value="" id="testo" name="testo">
Your form is within <script> tag, Place it outside of <script> tag.
and write following code within DOM ready like follwing:
<script type="text/javascript">
$(function() {
// after DOM ready
$('.divclass').click(function(){
var idarray = $(this).attr("id"); // or this.id do the same thing
$('#testo').val(idarray); // set value to testo
$("form#prova").submit(); // submit the form
});
});
</script>