I know this is a topic where you can find tons of answers for... I spent now quite some time to search for the solution I am looking for.
I have a PHP file with a form. This PHP file uses a JavaScript for a drop down pre-selection.
Means: If a user selects a value in drop-down1 then there is a limited selection in drop-down2.
This JavaScript definitely needs to reload the PHP file for changing the values of the second drop-down2 list.
The question is now: How can I keep the entered values of the user in the form, after the JavaScript has been executed. Currently all entered data get lost.
One option would be to set all values into the URL and grab them with GET. Yes.. that would be an option but as I am using 13 values, the URL would look not too nice. I don't want to user to take notice of what happens.
I cant use the POST, as I don't push the posting button.. it's just the JavaScript that gets executed and reloads the page.
I thought about filling the Session with the entered data but that is not directly possible as JavaScript is on the client side and the session is on the server side.
Do you have any suggestions?
Form Example:
First Name (text box) Last Name (text box) Age (drop-down) Sex (drop-down)
Province (JS drop down) District (JS drop down) Commune (JS drop down) Village (JS drop down)
Transf.from (drop down) Health Center (text box) Ambulance (drop-down) Self.transferred (drop down) SEND BUTTON
dynmicoptionlist.js code:
//Set parameters for provinces selection
function reload1(form) {
var val1=form.provinces.options[form.provinces.options.selectedIndex].value;
//Get the current URL
var url1=window.location.href;
//Check if the current URL has the search term in it (-1 means it is not in it)
if(url1.search("&provinces=") != -1) {
//Now that the search term was found, cut the search term from the URL so that it can be replaced
//This is necessary for multiple selections in the same drop down box
var url2 = url1.substr(0,url1.search("&provinces="));
//If the user has selected "Please select", then dont add the provinces parameter
if(val1 == "") {
//Create the new URL
self.location= url2
}
else {
//Create the new URL
self.location= url2 + "&provinces=" + val1 ;
}
}
else {
//The search term was not found, so just add the provinces
self.location= url1 + "&provinces=" + val1;
}
}
//Set parameters for districts selection
function reload2(form) {
var val1=form.provinces.options[form.provinces.options.selectedIndex].value;
var val2=form.districts.options[form.districts.options.selectedIndex].value;
//Get the current URL
var url1=window.location.href;
//Check if the current URL has the search term in it (-1 means it is not in it)
if(url1.search("&districts=") != -1) {
//Now that the search term was found, cut the search term from the URL so that it can be replaced
//This is necessary for multiple selections in the same drop down box
var url2 = url1.substr(0,url1.search("&districts="));
//If the user has selected "Please select", then dont add the provinces parameter
if(val2 == "") {
//Create the new URL
self.location= url2
}
else {
//Create the new URL
self.location= url2 + "&districts=" + val2 ;
}
}
else {
//The search term was not found, so just add the districts
self.location= url1 + "&districts=" + val2;
}
}
//Set parameters for communes selection
function reload3(form) {
var val1=form.provinces.options[form.provinces.options.selectedIndex].value;
var val2=form.districts.options[form.districts.options.selectedIndex].value;
var val3=form.communes.options[form.communes.options.selectedIndex].value;
//Get the current URL
var url1=window.location.href;
//Check if the current URL has the search term in it (-1 means it is not in it)
if(url1.search("&communes=") != -1) {
//Now that the search term was found, cut the search term from the URL so that it can be replaced
//This is necessary for multiple selections in the same drop down box
var url2 = url1.substr(0,url1.search("&communes="));
//If the user has selected "Please select", then dont add the provinces parameter
if(val3 == "") {
//Create the new URL
self.location= url2
}
else {
//Create the new URL
self.location= url2 + "&communes=" + val3 ;
}
}
else {
//The search term was not found, so just add the communes
self.location= url1 + "&communes=" + val3;
}
}
On behalf of JohnP's suggestion here are my changes so far:
I've added the onload function to fill the hidden field with a value:
<body onload=\"setValue()\">
The JavaScript filling the value looks like this:
<script type=\"text/javascript\">
function setValue() {
document.getElementById(\"dyndrpdwnhidden\").value=\"createPatient\";
}
</script>
In my form I've added the hidden field:
<input type=\"hidden\" id=\"dyndrpdwnhidden\">
To get the values out of the form's input fields in my JavaScript dynmicoptionlist.js above I've used as an example:
var entry_date = form.entry_date.value;
Maybe you try to use SESSION? The session data will not disappear even on page refresh or redirect.
To keep any data:
session_start();
$_SESSION['data']=$_POST['data_from_previous_page'];
To read it at any page:
session_start();
$_SESSION['data'];
To clear all session data:
unset($_SESSION);
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
session_destroy();
Why not just make your JS submit the form? That will make the variables available, and you can activate the second dropdown by looking at the post data instead of the $_GET data that the JS sends.
use localStorage https://www.w3schools.com/jsref/obj_storage.asp
Storage Object
The Storage object of the Web Storage API provides access to the session storage or local storage for a particular domain. This allows you to read, add, modify, and delete stored data items.
Related
Im probably overlooking something very obvious but at this point i surrender and need help.
Here is my situation. When my page loads, in a while loops, PHP generates a table with data from my server. Each table row has a name column, phone, etc. One of the columns is an icon that when clicked allows the user to view a popup with notes on this particular lead. Easy stuff.
The icons in each row have the same class name and their ID's are unique.
I have an AJAX request that should be pulling the notes data from the server and displaying it in the popup when the user clicks on the relative icon. I am trying to use $('.class').click(this).attr('id'); to set a variable in my AJAX request with the id that needs to be submitted to my PHP script.
PROBLEM: The AJAX request and return seems to be working fine but no matter which row icon I click on it only displays the data that belongs to the first row, or the first instance with the class name 'homelead' Example: I click on row 1 icon and i get a popup with row 1's notes, GREAT!. I click on any other row and it only shows the 1st rows data, :(. I have confirmed that the ID's associated with each row icon are correct by doing a simple click.(this).(id) and alerting the id belonging to the row icon. All is correct, just can't seem to get the JS variable to update with the correct ID.
Im confused why this is. Any help would be appreciated. Here is my current code.
HTML:
<td>
<img class="homelead" id="<?php echo $leadsfetch['unit_id'];?>"
onclick="ajax_unitnotes();" src="images/list-view.png">
</td>
<?php echo "</tr>"; } ?>
AJAX request:
function ajax_unitnotes(){
var hr = new XMLHttpRequest();
var url = "PHP/getnotes.php";
// this variable should update with clicked rows id before submitting to PHP script
var unitidnotes = $('.homelead').click(this).attr('id');
var vars = "unitidnotes="+unitidnotes;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("unitnotes").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("unitnotes").innerHTML = "processing...";
}
As you are using an onclick trigger in the tag itself - which is usually un common when using jQuery. You can do this:
<img class="homelead" id="<?php echo $leadsfetch['unit_id'];?>"
onclick="ajax_unitnotes($(this));" src="images/list-view.png">
And the in your function
function ajax_unitnotes(e){
var unitidnotes = e.attr('id');
}
Your current code
var unitidnotes = $('.homelead').click(this).attr('id');
Actually does not know what it the this object you are trying to access.
Better yet you can use a jQuery event, remove the onclick from the img tag and have an event like this:
$('.homelead').click(function(){
id = $(this).attr('id');
});
You could pass the clicked object this to the function trigged by "onclick":
onclick="ajax_unitnotes(this);"
That will make the DOM object you clicked on available inside the JS function.
You need to change the function signature accordingly:
function ajax_unitnotes(clickedElement){
and then you can alter this
var unitidnotes = $('.homelead').click(this).attr('id');
to
var unitidnotes = clickedElement.id;
This will give you the value of $leadsfetch['unit_id'] = img id.
I have a web page that allows authorized users to update or delete rows in a MySQL table. The table has the columns id (INT), label (VARCHAR), details (VARCHAR), templateId (INT), and auditable (TINYINT, either 0 or 1). This table is displayed as HTML on the front end, with "Label", "Details", "Auditable?" and "Edit/Delete" columns showing.
Clicking the "Edit" button on a row, changing some data in the resulting Bootstrap modal form, and clicking "Save Changes" works fine. The problem occurs when I click "Edit", click "Cancel" on the form, and then click another row (say, for example, I accidentally clicked the wrong row). When I click the button to execute the edit on that newly clicked row, both that row and the initially clicked row will be affected. The Chrome console shows that two JavaScript objects are being sent via $.post(), though I cannot figure out why from the logic I wrote (see below). I looked in MySQL and the duplicate results are there, confirming the page accurately reflects the update. Is there some $.get or $.post cache behavior in jQuery I am not aware of? (This also occurs with the delete functionality, but I'm limiting the question for brevity's sake).
Main page (GUI):
// The 'edit this row' button that brings up the modal form
$(".edit-action").click(function() {
// Clear any previously set values in form
$("#editActionLabel").val('');
$("#editActionDetails").val('');
$(".radio-edit-action").prop("checked", false);
// All edit button id's in GUI table will be "edit-action-[`id` in database]"
// Grab number out of there and convert from string to number
var actionId = $(this).attr("id");
actionId = parseInt(actionId.slice(12));
// Grab data from database to populate fields
$.get("data.php?a=actionData&actionId=" + actionId, function(d) {
// Returning a JSON encoded array isn't working,
// so I'm sending back a comma-separated string instead
var response = d.split(",");
var label = response[0];
var details = response[1];
var auditable = parseInt(response[2]);
$("#editActionLabel").val(label);
$("#editActionDetails").val(details);
if (auditable == 1) {
$("#editAuditableNo").prop("checked", false);
$("#editAuditableYes").prop("checked", true);
} else if (auditable == 0) {
$("#editAuditableYes").prop("checked", false);
$("#editAuditableNo").prop("checked", true);
}
// Only reset `auditable` variable if selection was changed
$(".radio-edit-action").change(function() {
auditable = $(this).val();
auditable = parseInt(auditable);
});
// User clicks "Save Changes" instead of "Cancel"
$("#executeEdit").click(function() {
var label = $("#editActionLabel").val();
var details = $("#editActionDetails").val();
var obj = {
"operation": "edit",
"actionId": actionId,
"label": label,
"details": details,
"auditable": auditable
};
console.log("The object passed to 'edit' this row:");
console.log(obj);
$.post("data.php", obj, function(r) {
// Confirm success or failure to user
$("#crudResult").html(r);
});
}); // end click
});
}); // end 'edit action'
data.php (called via AJAX to execute the UPDATE in database. Only relevant code shown):
$debug = false;
$operation = $_POST['operation'];
$action_id = (isset($_POST['actionId']) ? $_POST['actionId'] : '');
$label = (isset($_POST['label']) ? $_POST['label'] : 'NULL');
$details = (isset($_POST['details']) ? $_POST['details'] : 'NULL');
$auditable = (isset($_POST['auditable']) ? $_POST['auditable'] : 'NULL');
switch ($operation) {
case 'edit':
$query = "
UPDATE actions
SET label='$label',
details='$details',
auditable=$auditable
WHERE id=$action_id
LIMIT 1";
// DB connection not shown. Yes, I know I should be using PDO...
$result = mysqli_query($db_conn, $query);
// PHP echoes out the result; let the calling JavaScript figure out where to place it
if ($result) {
echo '<p class="text-success">Action successfully updated!</p>';
} else {
echo '<p class="text-warning">There was an error and the action could not be edited.</p>';
// Don't show error to user in production, when $debug should be false
if ($debug) {
echo '<p><b>Error:</b> ' . mysqli_error($db_conn) . '</p>';
}
}
break;
/* case 'delete': ... */
}
The modal form, which follows Bootstrap's template HTML, is simply a collection of fields and a couple buttons (no <form> wrapped around it). Please let me know if I can clarify anything.
How many times is the request to server occurs? I Bet it is twice.
The problem is on the client side.
For each edit click you have made, a new save click function has been created.
You do not need to add this for every edit click, take it out from the edit click function.
Hope it helps.
The attached picture shows the results page of the search engine that I'm building. For each return result, the user may click on the result (i.e. "Food Science") and it will expand out accordion-style to reveal information about that particular result.
I want to log each time the user clicks on a result (for learning/intelligence purposes) and store it in a database table that I have created which stores the session ID, the query, the position of the result, and the order in which the user clicked the item.
Using JQuery, I already have a function that will pull the title of the result that was clicked, and I have it set where I want to log the click, but I don't know how to do it since JQuery is client side and PHP is server side.
How can I use the JQuery to trigger a PHP function so that I can query the database to insert the click logs into my table?
Below is the JQuery function.
$(document).ready(function() {
$('.accordionButton').click(function(e) {
if($(this).next().is(':hidden') == true) {
$(this).addClass('on');
$(this).next().slideDown('normal');
$(this).next().slideDown(test_accordion);
// SEND CLICK ACTION TO LOG INTO THE DATABASE
alert($(this).find('h3:last').text()); // displays the title of the result that was just clicked
}
else {
$(this).removeClass('on');
$(this).next().slideUp('normal');
$(this).next().slideUp(test_accordion);
}
});
}
You can do something like this (untested):
Define a javascript variable to track the order of the clicks, outside your click function:
var order = 0;
Add this into your click function, at the bottom:
order++;
var sessionID = $("input[name='sessionID']").val(); // assuming you have sessionID as the value of a hidden input
var query = $("#query").text(); // if 'query' is the id of your searchbox
var pos = $(this).index() + 1; // might have to modify this to get correct index
$.post("logClick.php", {sessionID:sessionID, query:query, pos:pos, order:order});
In your php script called "logClick.php" (in the same directory):
<?php
// GET AJAX POSTED DATA
$str_sessionID = empty($_POST["sessionID"]) ? '' ; $_POST["sessionID"];
$str_query = empty($_POST["query"]) ? '' ; $_POST["query"];
$int_pos = empty($_POST["pos"]) ? 1 ; (int)$_POST["pos"];
$int_order = empty($_POST["order"]) ? 1 ; (int)$_POST["order"];
// CONNECT TO DATABASE
if ($str_sessionID && $str_query) {
require_once "dbconnect.php"; // include the commands used to connect to your database. Should define a variable $con as the mysql connection
// INSERT INTO MYSQL DATABASE TABLE CALLED 'click_logs'
$sql_query = "INSERT INTO click_logs (sessionID, query, pos, order) VALUES ('$str_sessionID', '$str_query', $int_pos, $int_order)";
$res = mysql_query($sql_query, $con);
if (!$res) die('Could not connect: ' . mysql_error());
else echo "Click was logged.";
}
else echo "No data found to log!";
?>
You can add a callback function as a third parameter for the $.post() ajax method if you want to see if errors occured in the script:
$.post("logClick.php", {sessionID:sessionID, query:query, pos:pos, order:order},
function(result) {
$('#result').html(result); // display script output into a div with id='result'
// or just alert(result);
})
);
EDIT: If you need the value of the order variable to persist between page loads because you paginated your results, then you can pas the value of this variable between pages using either GET or POST. You can then save the value in a hidden input and easily read it with jQuery. (Or you could also use cookies).
Example (put this in every results page):
<?php
$order = empty($_POST["order"]) ? $_POST["order"] : "0";
$html="<form id='form_session' action='' name='form_session' method='POST'>
<input type='hidden' name='order' value='$order'>
</form>\n";
echo $html;
?>
In your jQuery, just change var order = 0; to
var order = $("input[name='order']").val();
Then, when a user clicks on a page link, prevent the default link action, set the order value and the form action, and then submit the form using javascript/jQuery:
$("a.next_page").click(function(event) {
event.preventDefault();
var url = $(this).attr("href");
$("input[name='order']").val(order);
$("#form_session").attr('action', url).submit();
});
All the 'next' and 'previous' pagination links must be given the same class (namely 'next_page' (in this example).
EDIT: If your pagination is as follows:
<div class='pagination'>
<ul><li><a href='page1.url'>1</a></li>
<li><a href='page2.url'>2</a></li>
</ul>
</div>
then just change this:
$("div.pagination a").click(function(event) {
etc.
This one is pretty easy, you need a PHP-Script to handle AJAX requests which are sent from your Search page.
In your search page you'll need to add an .ajax to create an AJAX request to your Script.
Everything you need to know about AJAX can be found here: http://api.jquery.com/jQuery.ajax/
In your PHP-Script you'll handle the Database action, use GET or POST data to give the script an ID over Ajax.
Use Ajax. Write a simple php-script that writes clickes to the database. I don't know how you log the clicks in the database exactly, but you can send the clicked item unique identifier to a php script with ajax, for example via POST variables.
A little example, on click:
$.post(
'count_click.php',
{ id: "someid" },
function(data) {
// data = everything the php-script prints out
});
Php:
if (isset($_POST['id'])) {
// add a click in the database with this id
}
Send a request to a PHP page using jQuery AJAX. See here for more info (it is really simple):
http://api.jquery.com/jQuery.ajax/
In this particular case, as you do not need to return anything, it may be better to just use the POST or GET methods in jQuery:
http://api.jquery.com/jQuery.post/
http://api.jquery.com/jQuery.get/
Something like:
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston"
success: function(data){
alert('done');
});
Mostly i have 28 items, i used pagination and display in 3pages using Ajax
each page have 10 items, whatever i selected in check box it should display values at bottom ,every thing is OK right now, but my problem is when i select items in second page the previous list is disappearing , when i return back to fist page it is not showing previously selected items .
iam not getting how to do this
please help
thanks
i used this jquery code to get checked values
function showValues() {
var page = $("#pagedis").val();
var fields = $(":input").serializeArray();
$("#results_" + page).empty();
jQuery.each(fields, function(i, field) {
$("#results_" + page).append(field.value + "<br> ");
});
}
i need the action like gmail if we select 3 items in one page ,4 items in 2nd page ,when i come back the checked value will never chage
do your checkboxes all have the same name? If not, name them all the same.
make sure each checkbox has a unique value attribute
attach a handler to keep track of the checkboxes checked in an array
:
// global variable somewhere
var checkedBoxes = new Array();
$('input[name=theNameYouDefinedAbove]').click(function(event){
checkedBoxes[$(this).val()] = $(this).is(':checked');
});
Now, when you paginate, just do this
:
$('input[name=theNameYouDefinedAbove]').each(function(index, checkbox){
if (checkedBoxes[$(checkbox).val()]) {
// NOTE: choose ONLY ONE!
// for jQuery 1.6+
$(checkbox).prop('checked', true);
// for all jQuery
$(checkbox).attr('checked', 'checked');
}
});
Honestly, I'm not even sure the best way to go about this, but essentially, I have a function in an include file that takes a $type parameter and then will retrieve/print results from my db based on the $type passed into it... What I'm trying to do is have a series of links on a page that, when you click on a certain link, will run the function and display the results accordingly...
So, on the initial load of the page, there is a table that displays everything (and I'm simplifying the table greatly...)
<table>
<tr><th>Item</th><th>Type</th></tr>
<tr><td>Milk</td><td>Dairy</td></tr>
<tr><td>Yogurt</td><td>Dairy</td></tr>
<tr><td>Chicken</td><td>Meat</td></tr>
<tr><td>Zucchini</td><td>Vegetable</td></tr>
<tr><td>Cucumber</td><td>Vegetable</td></tr>
</table>
And, then, in a sidebar, I have a series of links:
Dairy
Meat
Vegetable
I'd like to filter the initial table (and back and forth, etc.) based on the link that is clicked, so that if the user clicks "Vegetable", the function from my include file will run and filter the table to show only "Vegetable" types...
The first idea that comes to mind is to add a class attribute to the <tr> tags and id attribs to the <a> tags so that you can easily filter that way:
<tr class="dairy"><td>Milk</td><td>Dairy</td></tr>
<tr class="meat"><td>Chicken</td><td>Meat</td></tr>
Dairy
Meat
Then in your JavaScript (I'm using jQuery here):
$('a').click(function(evt){
var myId = $(this).attr('id');
$('tr').each(function(idx, el){
if ($(el).hasClass(myId))
{
$(el).show();
}
else
{
$(el).hide();
}
});
});
This has the added benefit of allowing you to localize the text without having to change your code.
Ok I created a proper answer. You can do it the way Darrel proposed it. This is just an extension for the paging thing to avoid cookies:
$('a').click(function(evt){
var myId = $(this).attr('id');
// append a idndicator to the current url
var location = "" + document.location + "";
location = location.split('#',1);
document.location = location + '#' + $(this).attr('id');
//append to next and previous links
$('#nextlink').attr({
'href': $('#nextlink').attr('href') + '#' + $(this).attr('id')
});
$('#previouslink').attr({
'href': $('#previouslink').attr('href') + '#' + $(this).attr('id')
});
$('tr').each(function(idx, el){
if ($(el).hasClass(myId))
{
$(el).show();
}
else
{
$(el).hide();
}
});
});
Some code that is executed after page load:
var filter = window.location.hash ? '[id=' + window.location.hash.substring(1, window.location.hash.length) + ']' : false;
if(filter)
$('a').filter(filter).click();
This simulates/executes a click on page load on the link with the specific id.
But in general, if you have a large database, you should filter it directly with SQL in the backend. This would make the displayed table more consistent. For example if page 1 may only have 3 rows of class 'dairy' and on page 2 10 of class 'dairy'.
If youre printing out the whole tabel up front there is no need to go back to the server you can simple hide all teh rows of a given type. For example with jQuery:
$('#sidebar a').click(function(){
// grab the text content of the a tag conver to lowercase
var type = $(this).text().toLowerCase();
/* filter all the td's in the table looking for our specified type then hid the
* row that they are in
*/
$('#my_data_table td').contents().filter(function(){
return this.nodeType == 3 && this.toLowerCase() == type;
}).parent('tr').hide();
return false;
});
Really though the suggestion abotu adding a class to the TR is better because filtering on text content can get tricky if there is content youre not expecting for some reason (hence my conversion to all lower case to help with this).