send value from php to jquery function - php

I'm creating an employee scheduler application. I want to create a button to change the calendar view to next or previous month view. But the problem is, when I click the button, the calendar changed, but the employee which has been selected to be viewed on calendar is going back to default.
I have this button in my php file:
<input type="submit" id="month-next" value="Next Month">
and I have created jquery for my button:
$('input#month-next').click(function(id){
if(month==12){
++year;
month=0;
}
++month;
$('div#month-data').load('scheduler/ajax/calendar.php?month='+month+'&year='+year+'&empid='+id);
});
so, how can I pass a my 'empid' value to my jquery function from the php page?

If your jquery in same php page you can use
$('input#month-next').click(function(){
var id = '<?php echo $empid ?>';
//your remaining code
});
If your jquery at another place you need to pass as parameter in function call:
<input type="button" id="month-next" value="Next Month" onclick="monthNext('<?php echo $empid; ?>');" >
Use a function instead of click event as above:
function monthNext(id)
{
if(month==12)
{
++year;
month=0;
}
++month;
$('div#month-data').load('scheduler/ajax/calendar.php?month='+month+'&year='+year+'&empid='+id);
}

Use button instead of input type=submit because submit will reload page.

With HTML 5 you can use data attributes.
<input type="submit" id="month-next" value="Next Month" data-empid="5">
JS:
$('input#month-next').click(function(){
id = $(this).data("empid");
// rest of your code
}

take a hidden field where you shore the employee id and after click on the button get that hidden field value like this
$('#button').click(function(){
emp_id = $('#emp_id').val();
// rest of your code
}

Related

Insert Back Button in Form

I feel like this is simple but i'm not sure where to start.
I have a multi-page form that the user clicks 'next' through to get onto the next page. I would like to also give them a 'back' button so that they can go back and change their response if they need to. How do I insert a 'back' button that still keeps their data on previous pages?
I don't need to track what their response was before or anything like that, just the final response is all that's necessary.
My form has a lot of code, so if you need to see another example please let me know:
Code at the very start of form:
<form action="surveysubmit.php" method="post" enctype="multipart/form-data">
Code to proceed to 'next' page and where I need the 'back' button:
<input class="submit-next" type="submit" name="submit_second" id="submit_second" value="" />
Code to submit final page (and entire form):
<input class="submit-end" type="submit" name="submit_ninth" id="submit_ninth" value="" />
If on clicking the "next" button the user is redirected to a new page each time then you can use localStorage to store the values which the user entered in the previous page,
For this edit the submit button in your each page as follows,
<input class="submit-next" onclick="saveValues()" name="submit_second" id="submit_second" value="" />
Your JavaScript code,
function saveValues(){
//fetch all the form values
var name = document.getElementById("inputName").value;
//Store it in the localStorage
localStorage.setItem("name", name);
//Now submit the form
document.getElementById("myForm").submit();
}
Now on every page you have to check whether their are any set localStorage values,
window.document.onload = function(e){
var name = localStorage.getItem("name");
document.geteElementById("inputName").value = name;
}
To display back button,
<button onclick="goBack()">Go Back</button>
<script>
function goBack() {
window.history.back();
}
</script>

how to get value of two normal button in a single form?

I have a form in which i have two input button one for multiple delete and another for multiple suspend items. like this-
<form>
<input type="checkbox" name="check_item[]" value="<?php echo $users['user_id']; ?>" />
<input type="button" id="suspendall" name="suspendall" value="Suspend" />
<input type="button" id="deleteall" name="deleteall" value="Delete" /></td>
</form>
when I click on delete or suspend it ask for confirm that event by jquery like-
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?')){
$('#listing').submit();
}
});
if it confirm cancel form is not submitted and if it confirm OK form is submitted and on submission i have to delete or suspend that item from db. I have write this code for this-
if(isset($_POST['deleteall'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->deleteUser($usersId);
}
if(!empty($_POST['suspend'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->suspendUser($usersId);
}
the problem I am facing is both times when the form is submited i got only array of ids of check boxes. I am not able to identify which button is clicked because I am not getting button value. that why its not working, and if I changed these button into submit button its working very nice but didn't ask for confirm the event. What should I do for that. Do anyone have any solution for that, Please help me. thanks
When submitting with jQuery().submit() the button on which was clicked is lost.
You could try to not submit() the form in the click handler but instead call evt.preventDefault() when ! confirmed().
In you HTML form has a hidden form field.
On the JS event set the value of the hidden field before submitting
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?'))
{
$("#hiddenFormId").val("deleteAll");
$('#listing').submit();
}
});
Then use this type of code in your PHP (Sorry not a PHP programmer)
if $_POST['hiddenFormId'] == 'deleteAll'

Is there a way to get an HTML page to "remember" which button was pressed on a previous page?

I have a page with three buttons and I'd like to be able to "remember" which button was pressed on a later page. The sequence of events goes something like this:
page1.html: "Please click a button." BUTTON 1, BUTTON 2, BUTTON 3
page2.html: "Thanks for clicking a button. click for next page."
page3.html: "You selected BUTTON (x) from page 1."
I'm not sure how to go about this. Any help will be much appreciated.
Use cookies.
PHP:
setcookie(name,value,expiration);
I think you are looking for a tutorial on how to doing this, a great website to get started is w3schools
You can also use a form on each page and use hidden inputs to remember anything you want from a previous page, by using a little bit of javascript. Here is the principle (untested), modify for your own case:
<form name='session_form' action='' method='POST'>
<input type='hidden' name='button_name' value=''>
</form>
<input type='button' name='button1' value='BUTTON 1' onClick='go(this);'>
<input type='button' name='button2' value='BUTTON 2' onClick='go(this);'>
<input type='button' name='button3' value='BUTTON 3' onClick='go(this);'>
<script type='text/javascript'>
function go(button) {
var f = document.forms.form_session;
var bname = button.name;
f.button_name.value = bname;
f.action = 'page' + bname[-1] + '.php';
f.submit();
}
// or if you have loaded jQuery, drop the `onClick` attribute and use something like this:
$('input[type=button]').click(function(e) {
var bname = $(this).attr('name');
$('input[name=button_name]').val(bname);
var action = 'page' + bname[-1] + '.php';
$('form[name=session_form]').attr('action', action).submit();
});
</script>
In PHP (server-side) you can then read the name of the clicked button using $_POST["button_name"].

Transfer data from onClick into $_POST

I call some links (opening table in the div) in the form
<A HREF="#1" onClick = ?????>button 1<A>
<A HREF="#2" onClick = ?????>button 2<A>
<A HREF="#3" onClick = ?????>button 3<A>
I would like through the onClick function to send data (numbers: 1, 2, 3) and receive it in PHP in the same document. I guess I have to commit to this form.
How to do it?
EDIT -------------------------------------
I try the #gilly3 way
<script language="JavaScript">
function submitValue (n) {
var f = document.forms.myform_1;
f.myNumber.value = n;
f.submit();
}
</script>
<?php
global $PHP_SELF;
echo "<form action='". htmlentities($PHP_SELF)."' method=\"POST\" id=\"myform_1\">";
?>
<input type="hidden" name="myNumber" />
button 1
button 2
button 3
</form>
tested - working ok. thnks for your help
Add hidden fields to your form. In your click handler, write whatever value you want to the hidden fields and call form.submit().
function submitValue (n) {
var f = document.forms.myForm;
f.myNumber.value = n;
f.submit();
}
Use it like this:
<input type="hidden" name="myNumber" />
button 1
button 2
button 3
Or get the value from $_GET and skip the JavaScript:
button 1
button 2
button 3
use jQuery
$('#anchorid').click(function(){
$.post('self.php',{data:'1'},function(){
});
});

onclick disable submit button

i wanna disable a submit button when onclick. im able to disable the button but i cant submit the post value to php.
<input type="submit" onclick="this.disabled = true" value="Save"/>
or ref this
If you disable an input, then its value naturally won't be included in the form data. You'll need to disable the button after you submit. If you bind a callback to onclick, then it runs before the form submits.
What you need is something like this:
jQuery:
$(document).ready(function() {
$(document).unload(function() {
$('#submit-btn').attr('disabled', 'disabled');
});
});
Regular JS:
document.onunload = disableSubmit;
function disableSubmit() {
/* disable the submit button here */
}
Basically, instead of binding to the submit button's onclick event, this binds the disabling code to the document's unload event (document.onunload), which gets fired once the form is submitted and you begin to leave the page.
I solved it with simple jQuery. The code removes the button on click, then appends the fake button or some like "loading.." text and finally sends the form.
HTML:
<div class="holder"><input type='submit' value='ACCEPT' class='button'></div>
jQuery:
$('.button').click(function() {
$('.button').remove();
$('.holder').append("//fake input button or whatever you want.");
$('.form').submit();
});
In diference with other methods like unload the button changes in the instant moment you click and sends the form. With heavy forms i think is a better practice.
Using jQuery, add onClick handler that returns false:
<input type="submit" value="Submit" onClick="$(this).click(function() {return false;});"/>
i found a alternative online. wat i did is to create a fake disable and hidden button. when the actual button is clicked, i will hide it and show the fake disable button.
actual button:
$onclick = "
var boolconfirm = confirm('$strconfirmattempt');
if(boolconfirm==true){
finishattempt.style.display='none';
finishattempt2.style.display='inline';
}
return boolconfirm;";
fake button:
echo "<input type=\"submit\" name=\"finishattempt\" value=\"submit\" onclick=\"$onclick\" />.
<input type=\"submit\" id=\"finishattempt2\" name=\"finishattempt2\" value=\"submit\" style=\"display:none;\" DISABLED/>\n";
You could use a hidden field which would hold the value of the button and pull that value out of your POST data:
<input type="hidden" id="hiddenField" value="default" />
<input type="button" id="myButton" onclick="buttonClick();">
function buttonClick()
{
document.myForm.myButton.disabled = true;
document.myForm.hiddenField.value = "myButtonClicked";
}
My PHP is a little rusty, but then you can access the hidden field like so:
if ($POST['hiddenField'] == "myButtonClicked")
{
// Click handling code here
}
Why not create a disabled submit button that is hidden, and an active submit button, and onClick show the disabled and hide the active? I could do this in jQuery, but I'm kinda useless without it. Sad, eh?
Here's a method using onsubmit instead of onlick:
This goes at the top:
<script type='text/javascript'>
function disableButtons()
{
$('input[type="submit"]').attr('disabled', true);
}
</script>
Then your PHP (note that isset post is NOT for the submit button, because we want to disable the submit button).
if (isset($_POST['dothis']))
{
//CODE TO EXECUTE
}
Then HTML.
<form method='post' action='' onsubmit='disableButtons()'>
<input type='hidden' name='dothis' value=''>
<input type='submit' value='Submit'></form>
Onsubmit goes in .
Make sure your isset (the PHP part) is for an input that goes with your submit, but is not the submit button itself. You can see that it is the hidden value being checked for with the PHP, rather than the submit button, and the submit button is what gets disabled.
By doing this, you can disable a submit button without disabling the PHP.

Categories