i am trying to submit form after 10 seconds with form value. I am not able to include setTimeout with submit function.
setTimeout(function() {
$('#FrmID').submit();
}, 10000);
$(document).ready(function() {
$("#submit").click(function() {
var grp_id = $("#grp_id").val();
var datastr = 'grp_id=' + grp_id;
$.ajax({
type: 'POST',
url: 'start_calculate.php',
data: datastr,
success: function() {
//$("#msg").html("Student Successfully Added");
//$("#msg").html("response");
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
<input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
<button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>
You can create a function for submit, then call it after click on success:
$(document).ready(function() {
function submitForm() {
setTimeout(function() {
$('#FrmID').submit();
}, 10000);
}
$("#submit").click(function() {
var grp_id = $("#grp_id").val();
var datastr = 'grp_id=' + grp_id;
$.ajax({
type: 'POST',
url: 'start_calculate.php',
data: datastr,
success: function() {
//$("#msg").html("Student Successfully Added");
//$("#msg").html("response");
submitForm();
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
<input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
<button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>
In Action:
$(document).ready(function() {
function submitForm() {
i = 1;
setInterval(function() {
console.log(i++);
}, 1000);
setTimeout(function() {
$('#FrmID').submit();
alert("Student Successfully Added");
}, 10000);
}
$("#submit").click(function(e) {
e.preventDefault();
submitForm();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
<input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
<button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>
And if you want to submit form automatically without press any key to submit you can try this. You should move your code inside the setTimeout function:
$(document).ready(function() {
setTimeout(function() {
$('#FrmID').submit(function() {
var grp_id = $("#grp_id").val();
var datastr = 'grp_id=' + grp_id;
$.ajax({
type: 'POST',
url: 'start_calculate.php',
data: datastr,
success: function() {
//$("#msg").html("Student Successfully Added");
//$("#msg").html("response");
}
});
});
}, 10000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
<input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
<button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>
Or you can try delay()
$(document).ready(function() {
$('#FrmID').delay(10000).submit(function() {
var grp_id = $("#grp_id").val();
var datastr = 'grp_id=' + grp_id;
$.ajax({
type: 'POST',
url: 'start_calculate.php',
data: datastr,
success: function() {
//$("#msg").html("Student Successfully Added");
//$("#msg").html("response");
}
});
});
});
The problem with your code is that you've not bound the submit handler to the form. Instead, you're binding it to the click event of the submit button.
You need to bind the handler code to the submit event of the form instead:
setTimeout(function() {
$('#FrmID').submit();
}, 10000);
$(document).ready(function() {
$("#FrmID").submit(function() {
var grp_id = $("#grp_id").val();
var datastr = 'grp_id=' + grp_id;
$.ajax({
type: 'POST',
url: 'start_calculate.php',
data: datastr,
success: function() {
//$("#msg").html("Student Successfully Added");
//$("#msg").html("response");
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="FrmID" id="FrmID">
<input type="hidden" value="<?php echo $grp_id; ?>" name="grp_id" />
<button type="submit" name="done" class="btn btn-sm btn-success" id="submit">Done !</button>
</form>
You can use following code.
$(document).ready(function() {
$("#submit").click(function() {
setTimeout(function() {
var grp_id = $("#grp_id").val();
var datastr = 'grp_id=' + grp_id;
$.ajax({
type: 'POST',
url: 'start_calculate.php',
data: datastr,
success: function() {
//$("#msg").html("Student Successfully Added");
//$("#msg").html("response");
}
});
}, 10000);
});
});
On click of submit just execute your code after delay.
Related
I am getting Id value from my AJAX and I am also able to display it. I set the Id value input text in the AJAX and displaying it on HTML like this <input value="4864" id="compare_id" type="hidden"> Now I have to pass that value to PHP to get the result from database.
Is there any idea how to do it?
AJAX
$(document).ready(function() {
arr = [];
$("[type=checkbox]").click(function() {
$.ajax({
type: "POST",
url: "includes/compare_process.php", //
data: 'users=' + arr,
dataType: 'json',
success: function(msg) {
$("#pics_name,#pics_Id").empty();
$.each(msg, function() {
$("#pics_Id").append("<input type=hidden value=" + this.Id + " id=compare_id name=compare_id>");
//alert(msg.id);
});
},
error: function() {
alert("failure");
}
});
});
});
//tried AJAX
$(document).ready(function(){
$('#search-button').click(function(){
$.ajax( {
type: "GET",
url: 'includes/compare_process.php?key=compare_details',
data: $('#search-form').serialize(),
success: function(response) {
//$('#response').html(response);
alert(response);
}
} );
});
});
PHP
<div class="bg-popup" style="display: none;" id="open_compare_popup">
//popup code here
<?php
$val2=htmlentities($_GET['compare_id']);
echo $val2;
$sql = "SELECT * FROM `request` WHERE Id IN($val2)";
?>
//end popup code here
This output I am getting from AJAX
<form action="#" method="get" id="search-form">
<span id="pics_Id">
<input value="4869" id="compare_id" name="compare_id" type="hidden">//output
<input value="4884" id="compare_id" name="compare_id" type="hidden">//output
<input value="5010" id="compare_id" name="compare_id" type="hidden">//output
</span>
<button class="action action--button action--compare" type="button" id="search-button" name="search-button"><span class="action__text">Compare</span></button>
</form>
I have to pass the input value to PHP after clicking on a button.
What I have here is ajax that's post information into textbox from database. This ajax work's in input field, but when I tried to use select box it doesn't working. Why is that?
not working
<select id="tag"><option value="">none</option><option value="crs">crs</option></select>
working
<input name="tag" id="tag" type="text" value="" />
Index.php
<select id="tag"><option value="">none</option><option value="crs">crs</option></select>
<input name="output" id="output" type="text" value="" />
<script type="text/javascript">
$(document).ready(function()
{
$('input[id="tag"]').change(function()
{
var prjt_code = $("#tag").val();
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"prjt_code="+prjt_code,
dataType:'html',
type:'POST',
success:function(data){
//alert(data);
$('#output').val(data);
}
});
return false;
});
});
</script>
autocomplete-ajax.php
<?php
if(isset($_POST['prjt_code'])) {
$prjt_code = $_POST['prjt_code'];
$sql = $mysqli1->query("SELECT * FROM project WHERE project='$prjt_code'");
while($row1 = $sql->fetch_assoc())
{
$code = $row1['project_code'];
}
echo $code;
}
?>
You are targeting input[id="tag"] when you want select[id="tag"]
http://jsfiddle.net/releaf/U28jb/
$(document).ready(function() {
$('select[id="tag"]').on('change', function() {
var prjt_code = $("#tag").val();
alert(prjt_code);
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"prjt_code="+prjt_code,
dataType:'html',
type:'POST',
success:function(data){
//alert(data);
$('#output').val(data);
}
});
return false;
});
});
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('form.ajax-form').onclick('login_submit', function() {
var obj = $(this), // (*) references the current object/form each time
url = obj.attr('action'),
method = obj.attr('method'),
data = {};
obj.find('[user_email]').each(function(index, value) {
// console.log(value);
var obj = $(this),
email = obj.attr('user_email'),
value = obj.val();
data[email] = value;
});
obj.find('[user_pass]').each(function(index, value) {
// console.log(value);
var obj = $(this),
pwd = obj.attr('user_pass'),
value = obj.val();
data[pwd] = value;
});
$.ajax({
// see the (*)
url: url,
type: method,
data: data,
success: function(response) {
console.log(response);
// $("#feedback").html(data);
}
});
// console.log('Trigger');
return false; //disable refresh
});
});
</script>
<?php echo form_open('welcome/login', array('class' => 'ajax-form','style'=>'z-index: 202')); ?>
<input name="user_email" type="email" class="login_email" placeholder="Email address" required autofocus>
<br />
<input name="user_pass" type="password" class="login_pass" placeholder="Password" required>
<label class="checkbox">
Forgot login details?
<button class="btn btn-lg btn-info" id="login_submit" style="margin-left:20px;">Sign in</button>
</label>
</form>
<?php if(isset($login_error) && $login_error !=='') { ?><div class="alert alert-danger login_err"><?php echo $login_error; ?></div> <?php } ?>
<!-- </div>-->
Hi i am trying to pass the email and password to the controller in codeigniter.
Any idea how to do that please help?
You could simplify a big deal by doing:
$('#ajax-form').submit(function(){
var form = $(this);
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
success: function(response) {
console.log(response);
// $("#feedback").html(data);
}
});
return false;
});
I have this CLICK function that works for one specific HTML button. I'd like to use it on multiple buttons, but each button needs to pass different variables to the same page.
BUTTON
<input type="button" id="scs" value="TEST" />
JQUERY
$("#scs").click(function() {
$("#status").html("<p>Please Wait! TESTING</p>");
$.ajax({
url: 'go.php?ab=1',
success: function(data, textStatus, xhr) {
alert (data);
}
});
});
How can I adapt this so if I have 4 more buttons each button uses the function, calls the same page but uses unique values ?
EG :
<input type="button" id="update" value="update" /> Calls go.php?up=1
<input type="button" id="new" value="new" /> Calls go.php?new=1
etc...
Thanks
Give your buttons the same class and call the code using the class name, and add a data attribute to each button to retrieve the seperate values.
Give this a try:
<input type="button" class="clickButton" data-value="go.php?up=1" value="Update" />
<input type="button" class="clickButton" data-value="go.php?new=1" value="New" />
$(".clickButton").click(function() {
$("#status").html("<p>Please Wait! TESTING</p>");
$.ajax({
url: $(this).attr("data-value"),
success: function(data, textStatus, xhr) {
alert (data);
$(this).prop('value', 'New Text');
}
});
});
Use the data-attribute to to set the url.
Use event delegation to target the buttons
<input type="button" class='btn' data-params='up=1' value="update" /> Calls go.php?up=1
<input type="button" class='btn' data-params='new=1' value="new" /> Calls go.php?new=1
$("button").on('click', '.btn', function() {
var url = 'go.php?' + $(this).data('params');
$("#status").html("<p>Please Wait! TESTING</p>");
$.ajax({
url: url,
success: function(data, textStatus, xhr) {
alert (data);
}
});
});
Use class for click event and new attribute to find the button like 'data-id'
<input type="button" class="clickevent" data-id="update" value="update" />
Jquery
`
$(".clickevent").click(function() {
$("#status").html("<p>Please Wait! TESTING</p>");
$.ajax({
url: $(this).attr('data-id'),
success: function(data, textStatus, xhr) {
alert (data);
}
});
});
`
You can check by the value of the clicked button and use a 'url' variable for the ajax request:
$("button").click(function() {
$("#status").html("<p>Please Wait! TESTING</p>");
var url='';
var value = $(this).val();
if(value == 'update'){
url='go.php?up=1';
} else if(value=='new') {
url='go.php?new=1';
} else if(value=='scs') {
url='go.php?ab=1';
}
$.ajax({
url: url,
success: function(data, textStatus, xhr) {
alert (data);
}
});
});
Use same class for each button
<input type="button" id="update" class="click" value="update" />
<input type="button" id="new" class="click" value="new" />
$(document).ready(function(){
$(".click").click(function(){
var val=$(this).val();
if(val =="update")
{
$.post("go.php",{'up':'1'},function(data){
alert(data);
});
}
else
{
$.post("go.php",{'new':'1'},function(data){
alert(data);
});
}
});
});
i have a website with a jquery search form. I have the searchboxes in every page of the site indide the header. Once you enter the searchwords there it redirects you the the search.php
What i want to do is to make the form submitted as soon as the page loads, because i send the data to the searchbox inside the search.php
Example: in page 1 i enter something and click submit. In search.php loads and in the main search field i have somethis(what i entered before). But i have to click again the submit to see the results. Any idea how can i make the form autosubmit when im being redirected? So i can see the results instantly.
Here's the code:
<form method="get" id="myForm" name="myForm" >
<input type="text" name="search" id="search_box" value="<?php $_GET['q']; ?>" placeholder="Enter band, artist, name..." autofocus/>
<input type="submit" value="Go" />
<br />
</form>
and the jquery:
<script type="text/javascript">
$(function() {
//-------------- Update Button-----------------
$(".search_button").click(function() {
var search_word = $("#search_box").val();
var dataString = 'search_word='+ search_word;
if(search_word==''){
} else {
$.ajax({
type: "GET",
url: "searchdata.php",
data: dataString,
cache: false,
beforeSend: function(html) {
document.getElementById("insert_search").innerHTML = '';
$("#flash").show();
$("#searchword").show();
$(".searchword").html(search_word);
$("#flash").html('<p style="text-align:center;">Loading Results...</p>');
},
success: function(html){
$("#insert_search").show();
$("#insert_search").append(html);
$("#flash").hide();
}
});
}
return false;
});
//---------------- Delete Button----------------
});
</script>
$(function(){
$('#myForm').trigger('submit');
});
When the page is ready you simply trigger the submit using jquery.
$(function() {
$(".search_button").click(function() {
var search_word = $("#search_box").val();
var dataString = 'search_word='+ search_word;
if(search_word==''){
} else {
$.ajax({
type: "GET",
url: "searchdata.php",
data: dataString,
cache: false,
beforeSend: function(html) {
document.getElementById("insert_search").innerHTML = '';
$("#flash").show();
$("#searchword").show();
$(".searchword").html(search_word);
$("#flash").html('<p style="text-align:center;">Loading Results...</p>');
},
success: function(html){
$("#insert_search").show();
$("#insert_search").append(html);
$("#flash").hide();
}
});
}
return false;
});
});
$(document).ready(function(){
$('#myForm').submit();
});
You can try this way.
<form method="get" id="myForm" name="myForm" >
<input type="text" name="search" id="search_box" value="" placeholder="Enter band, artist, name..." autofocus/>
<input id="button-submit" type="submit" value="Go" />
</form>
<script type="text/javascript">
$(function() {
function _myFunction () {
var search_word = $("#search_box").val();
var dataString = 'search_word='+ search_word;
if(search_word==''){
} else {
$.ajax({
type: "GET",
url: "searchdata.php",
data: dataString,
cache: false,
beforeSend: function(html) {
document.getElementById("insert_search").innerHTML = '';
$("#flash").show();
$("#searchword").show();
$(".searchword").html(search_word);
$("#flash").html('<p style="text-align:center;">Loading Results...</p>');
},
success: function(html){
$("#insert_search").show();
$("#insert_search").append(html);
$("#flash").hide();
}
});
}
return false;
}
$("#button-submit").click(function(e) {
e.preventDefault();
_myFunction();
return false;
});
_myFunction(); //This is where the magic happens
});
</script>
By doing that, the page will trigger on loading the function that is used when you click on the button.