update and save date automatically - javascript

Hi guys i am working for project in php I have <form> with save button <input> to update the data in the database
The idea what i want to do , instead to click every single page to update the data, i want to do it automatically by using for example javascript function for example every 10 min every single page will update auto
Manually if I click Save button it will update data in DB
once i click the page i can see there is some update and it need to save in db, i need to do this automatic click save button for every single pages every 10 min
Here is an example of what i did
<form action="/test.php" method="post">
<input type="text" name="test1" value="<?= $test->from; ?>" />
<input type="text" name="test2" value="<?= $test->to; ?>" />
<input type="text" name="test3" value="<?= $test->distance; ?>" />
<input type="text" name="test4" value="<?= $test->time; ?>" />
<input type="hidden" name="action" value="<?= $action ?>" />
<input type="hidden" name="id" value="<?= $test->id ?>" />
<input type="submit" name="submit" value="<?= $title ?>" />
</form>
And here for the script
window.onload(function () {
setTimeout("document.forms[0].submit();", 10000);
});
I need your help guys to solve this problem it will save my time instead of open every single page and click update button

Try this simple change in your script. I hope it will help.
function autoSubmit(){
setTimeout(document.forms[0].submit(), 10000);
}
window.onload = autoSubmit()

Related

How to declare variables dynamically in javascript

is it possible to read from post variables dynamically?
i.e.
function ajax_edit_color(color_id, building)
{
var color_name = $('#color_name_'+color_id).val();
var color_hex_code = $('#color_hex_code_'+color_id).val();
console.log(color_name, color_hex_code);
}
when I look at the console, the variables are undefined. Is there a correct syntax for this?
EDIT :
the HTML is dynamic (PHP):
<label for="color_name_<?php $value['id']; ?>">Color name : </label>
<input type="text" name="color_name_<?php $value['id']; ?>" id="color_name_<?php $value['id']; ?>" class="form-control" required="required" value="<?php echo $value['color_name']; ?>" />
<label for="color_hex_code_<?php $value['id']; ?>">Color HEX code : </label>
<input type="text" name="color_hex_code_<?php $value['id']; ?>" id="color_hex_code_<?php $value['id']; ?>" class="form-control" required="required" value="<?php echo $value['color_code']; ?>" />
AND HERE's the rendered HTML :
<label for="color_name_">Color name : </label>
<input type="text" name="color_name_6" id="color_name_" class="form-control" required="required" value="orange" />
<label for="color_hex_code_">Color HEX code : </label>
<input type="text" name="color_hex_code_6" id="color_hex_code_" class="form-control" required="required" value="ff9c00" />
Look at your final HTML:
<label for="color_name_">Color name : </label>
<input type="text" name="color_name_6" id="color_name_"
class="form-control" required="required" value="orange" />
The name is right, but the id is wrong. Fix your php, and the javascript will start working. The id needs to be color_name_6, not color_name_.
Your basic approach is correct, so I suggest you start with a simplified test, for example without PHP. One basic example, which I verified to work:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/libs/jquery-2.1.4.js"></script>
<script>
function test(){
var index=1;
var val=$("#color_name_"+index).val();
console.log( '****' +val);
};
</script>
</head>
<body>
<button onclick="test();return false;">test</button>
<input type="text" id='color_name_1' value='green'>
<input type="text" id='color_name_2' value='blue'>
</body>
This works. Now if your own code is failing, try looking for differences, in particular:
1. When is your javascript running - could it be before the "color_name" elements are rendered in the document?
2. Could your PHP be generating wrong values, this could be tested by viewing the document source of course
edit you javascript to match the following
function ajax_edit_color(color_id, building)
{
var color_name = $('#'+color_id).val();
var color_hex_code = $('#'+color_id).val();
console.log(color_name, color_hex_code);
}
//color_name_ and color_hex_code_ are both already part of your ids.

pass input value to another form on a diff url and run submit wordpress

I have 2 forms, 1st form is to submit an input value to the next page, and on the next page there's the 2nd form which is a search map function.
the 1st form is displayed on the homepage
<form role="search-map" method="" id="search-map" action="/find">
<h4>Where To Buy</h4>
<input type="text" class="form-control" id="addressInput" name="addressInput" placeholder="Your Suburb or Postcode">
<button type="submit" class="btn btn-vc btn-default btn-lg" id="search-address-btn">Find</button>
</form>
the 2nd form is on another page which is /find that has a map search plugin
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="where-i-want-value-to-be-pass">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>
any ideas how can i pass the value from the 1st form "addressInput" to the 2nd form? and the run the search on the 2nd form? here's the 2nd form btw
i tried searching here but what i need seems to be more complex that what i have found
or maybe how can i get the 2nd page to get the value from the url (?addressInput=North+Bega%2C+NSW%2C+2550) into the 2nd form input "addressInput" and run the submit button function when the page loads?
thanks in advance guys
I'm assuming you want to get the value from s to the second form.
replace where-i-want-value-to-be-pass
with <?php echo $_REQUEST['addressInput']; ?>
<form onsubmit="cslmap.searchLocations(); return false;" id="searchForm" action="">
<input type="text" id="addressInput" name="addressInput" placeholder="Your Suburb" size="50" value="<?php echo $_REQUEST['addressInput']; ?>">
<input type="submit" class="slp_ui_button" value="Find" id="addressSubmit">
</form>

Adding a item into a cart automatically using javascript

Hi i have a cart that i want to add a form automatically this code seems to kinda do the trick only the problem is i have to hit F5 for it to add the amount i am quite new to this and cant figure out where i have gone wrong.
<form method="post" action="level3.php" class="jcart" id="foo">
<fieldset>
<input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
<input type="hidden" name="my-item-id" value="ABC-8" />
<input type="hidden" name="my-item-name" value="Level 1" />
<input type="hidden" name="my-item-price" value="95.00" />
<input type="hidden" name="my-item-url" value="" />
<table>
<tr>
<td width="65%">
<strong>Level 1 all for £95</strong>
</td>
<td width="15%" align="right">
£95.00
</td>
<td>
<input type="text" hidden ="true" name="my-item-qty" value="1" size="3" hidden="true" />
</td>
<td width="20%" align="center">
<input type="submit" name="my-add-button" id="my-add-button" value="add" class="button" />
</td>
</tr>
</table>
<input type="hidden" name="visited" value="" />
</fieldset>
That is the form that submits the amount into the check out.
<script>
$(document).ready(function(){
//Check if the current URL contains '#'
if(document.URL.indexOf("#")==-1)
{
url = document.URL+"#";
location = "#";
} else {
location.reload(true);
document.getElementById("my-add-button").click();// Simulates button click this has to be in as it links to another piece of java that adds the item to the check out with out this it wont add the item
document.foo.submit(); // Submits the form without the button
}
});
</script>
document.getElementById("my-add-button").click();
The code above links to the code below that adds the items from to my knowledge
$('.jcart').submit(function(e) {
add($(this));
e.preventDefault();
});
Thank you in advance for any help or suggestions
If i understand correctly, you increase some value of cart in PHP and than want to update quantity on page without refreshing page.
I made little JSFiddle: http://jsfiddle.net/7SbBA/
Basically use ajax:
$.ajax({
url: 'example.com',
data: /* pass product id here */
success: function(){
/* update quantity you want */
}
});
There is some html change too.
UPDATE
If you want updated total values on page load, just £<span class='amount'><?php echo $product['quantity'] * $product['price'] ?></span> and there is no need to use jquery/js for that. But if you still want dynamically update on window load, i updated my JSFiddle v.2

Form onsumbit executing a function

I have this code:
function src(){
var id = document.getElementsByName("query")[0].value;
window.location.href = "/search/?query=" + id;
}
var f_src = document.getElementById("search_form");
if(f_src)
f_src.onsubmit = function(){src();return false;};
I'm having problems with the submit part: I want, when I enter "example" in the field "url" to be redirected to "/search/?query=example", but it's not working, any help?
HTML:
<form id="search_form" method="get" action="" name="search_f">
<input id="search_field" maxlength="100" name="query" type="text" value="" placeholder="<?php echo $src_txt; ?>">
<input id="search_button" type="submit" value="">
</form>
Thanks.
Aside from the problem you might have, you can achieve the same behavior without JavaScript. You just have to give the form element the name "query" and make it submit a GET request to /search:
<form id="search_form" method="get" action="/search/" name="search_f">
<input id="search_field" maxlength="100" name="query" type="text" value="" placeholder="<?php echo $src_txt; ?>">
<input id="search_button" type="submit" value="">
</form>
The action attribute tells the browser where to make the request to. Every form control element with a name will be included with its value in the query string.
No JavaScript required.
If You don't insist on using javascript, You can achieve this using good old HTML:
<form action="/search/" method="get">
<input name="query" />
</form>

Unset session variable on button click event

I can't unset a session variable properly using a "remove button" click. I have two pages: product page and shopping cart page, but problem is that I have a "remove button" in the shopping cart which is not working properly.
When I click on "remove button" it should run unset($_SESSION['product1'].
But when I select another item then it can display "previous session item" so it means remove button does not run unset($_SESSION['product1'] properly. How do I solve this issue?
Product Page
session_start();
$id=$_REQUEST['id'];
$_SESSION['pid1']= $_POST['ids']; //Product ID//
$_SESSION['product1'][]=$_POST['product'];
<form method="post">
<input type="hidden" name="product[]" value="<?php echo $row1['product']; ?>" />
<input type="hidden" name="ids" value="<?php echo $id?>" />
</form>
<input type="submit" class="button1" name="addtocart" value="Add To Cart"
/>
</form>
Shopping Cart Page
session_start();
$pid=$_SESSION['pid1'];
function remove_product($pid){
$pid=intval($pid);
$max=count($_SESSION['product1']);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['product1'][$i]['pid']){
unset($_SESSION['product1'][$i]);
break;
}
}
$_SESSION['product1']=array_values($_SESSION['product1']);
}
if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_SESSION['product1'][$pid]);
}
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
<input type="button" class="button2" value="Remove"
onclick="javascript:del(<?php echo $pid?>)"/>
Shopping Cart Page Javascript
<script language="javascript">
function del(pid){
if(confirm('Do you really mean to delete this item')){
document.form1.pid.value=pid;
document.form1.command.value='delete';
document.form1.submit();
}
}
function clear_cart(){
if(confirm('This will empty your shopping cart, continue?')){
document.form1.command.value='clear';
document.form1.submit();
}
}
</script>
It might be caused by
$pid=$_SESSION['pid1']; instead of $pid=$_POST['pid1'];
event that is tied to tag wrapped around a button, you coud just use
<input type="button" class="button2" value="Remove" onclick="del('<?php echo $pid?>');"/>

Categories