)
I work for pagination for my table, my problem is where I click "next" the javascript increase ( I show it with alert) and not php value, so I think the my page is not refresh
get php value :
<input type="hidden" name="page" value="<?=$this->page?>" id="page">
button next :
<li class="page-item" ><a class="page-link" id="page_sui" href="#" >Next</a></li>
javascript:
$("#page_sui").click(function{
var val = $("#page").val();
val++;
$("#page").val(val);
alert($("#page").val());
});
PHP values cannot be changed through Javascript. When you click your button you are calling a Javascript function, and Javascript functions can only change Javascript variables and DOM elements. If you want to navigate to the next page with your current code, change your Javascript to this:
$("#page_sui").click(function{
var val = $("#page").val();
val++;
$("#page").val(val);
window.location='mypage.php?page=' + val;
});
And then in your PHP file add this:
if (!empty($_GET['page'])) $this->page = intval($_GET['page']);
Lastly, do NOT use <?=, it's bad practice!
Your script is not communicating the request from the browser to the server. By writing value="<?=$this->page?>", the server code (php) is putting the value in the HTML for the browser (javascript) to interpret and use. By writing $("#page").val(val);, only the value in the browser is changing but it does not send this request to the server to get more data. To do this you need to either navigate to a URL using window.location from javascript, or use AJAX from javascript to load the content you want and then place it on the page using javascript.
Related
I set value of my input type hidden in ajax response and then I wanna display my page with set content.
Ajax :
var JSONObject = JSON.parse(data);
$("#hiddenSearchedTextValue").val(JSONObject[1]);
window.location = JSONObject[0]; // redirect on my page
on My page, where I wanna display my set value:
<script>
var value = "";
window.onload = function () {
$ = jQuery;
value = $("#hiddenSearchedTextValue").val();
}
</script>
<div class="container">
<h1> Searched for: <script>document.write(value)</script></h1>
</div>
<input type="hidden" id="hiddenSearchedTextValue" value="">
Now it returns nothing. Anyone have some advice for me, please? I tried to read something about global scope, but nothing helped. Thank you
Unless I'm misunderstanding from your code snippets you are getting a response from Ajax and then redirecting the page? If this is the case then you loose your value when the page is re-loaded i.e. once you have your response.
I think you need to pass the value as a parameter in your url, then set it on page load
I think you want to just update the value and then reset the input, something like (in PHP):
value = $("#hiddenSearchedTextValue").val();
.....
....
<input type="hidden" id="hiddenSearchedTextValue" value="' . $_GET['searchString'] . '">
(with some appropriate security checking etc). Although you might as well just do this in the h1 tag. Maybe seeing some more of your code and your Ajax would make it easier to understand how you think it should work
i am hoping someone will help me, i am pulling out my hair for the last few hours...!
I am using asp.net web forms with c#
I have the following button in my menu , this button opens up a page that lists selected properties for realtor…
htmlCode.Append("<button type='button' class='btn btn-basic' id='btnEnquire' onclick='openEnquiryPage()'> <span class='glyphicon glyphicon-th-list'></span> View my list </button>");
function openEnquiryPage() {
location.href = "EnquiryPage.aspx";
//get the list from the storage into the hidden field
//getListOfProperties();//not in use...
}
The list is stored in localstorage as Json, I can see the correct data in the text box on the page
var retrievedData = localStorage.getItem("propertyArray");
proplist2.value = retrievedData;
declared as follows in asp.net control
<asp:TextBox ID="TextBox1" runat="server" Text="initial"></asp:TextBox>
BUT in the hidden field, the data is always = ""
<asp:HiddenField ID="txtPropList" runat="server"></asp:HiddenField>
javascript to populate the hidden field...
function getListOfProperties() {
document.getElementById("body_content_txtPropList").value = retrievedData;
var proplist2 = document.getElementById("body_content_txtPropertyList");
proplist2.value = retrievedData;
}
when I debug the following lines in the code behind ...
Page.ClientScript.RegisterStartupScript(this.GetType(),"prop", "getListOfProperties();",true);
_propertyListJson = txtPropList.Value;
propertyListJson I always = “”
Thank-you in advance.!
If you want to use the hidden field then you should use
<%=txtPropList.ClientID%>
as on when you render the page and see it in inspect element server would render the different Id so it would not store the value in hidden field simply so it is feasible to use ClientID to create the static mode. But as on when you call the javascript function from the c# code then it would call the javascript function but it would not wait for it and execute the next line of code as on at that time the value is not store in hidden field so it would return the null value.
If you want then call the function of javascript on page load event and then click the button and retrieve the value it would successfully retrieve the value but i dont think that these would be the feasible code from my opinion
I suggest to create the WebAPI which would simply get the the data from the localstorage and perform the operation
And webAPI can be easily called from javascript using ajax call
I hope this would be helpful and answer is satisfactory 😊☺
Thank You ☺🙂
I'm looking to set a variable based on some security for the user logged into and then use that value within JQuery.
This is the setting the variable:
<c:set var="hasResDetailAccess" value="false"/>
What JQuery would be used to retrive that vlaue
jQuery(document).ready(
function() {
var display = $(#frank).text();
});
Actually what you are trying with <c:set> and jQuery both are different thing. <c:set> is useful while translating jsp to servlet i.e. on server side
and 'jQuery' is used to process value on the client side i.e. in the browser.
So if you want a variable within jQuery just add it to any scope i.e request,session,context or application as per your requirement and retrieve it in jsp.
For example in Servlet,
add
request.setAttribute("hasResDetailAccess",hasResDetailAccess);
And in JSP just put code as below,
jQuery(document).ready(
function() {
var hasResDetailAccess= ${hasResDetailAccess} ;
});
${hasResDetailAccess} will fetch value of the variable regardless of the scope and set it to the jQuery variable `hasResDetailAccess. And latter on you can retrieve that jQuery Variable in HTML page anywhere any point of time you need.
Try printing the value in a hidden input an retrieve it from there:
<input type="hidden" value="<c:out value="${hasResDetailAccess}"/>" id="hasResDetailAccess">
and retrieve it with
var display = $('#hasResDetailAccess').val();
When i pass a value to textbox using Javascript it is working fine for the first time before updating data, after the update table will be refreshed. Then I couldn't pass value to textbox in the same way, but its working if I refresh the browser.
<div id="div_for_values">Edit</div>
$(function(){
$("#div_for_values a").click(function(){
var key_id = $(this).attr("key_id");
var key_value = $(this).attr("key_value");
$('#txt_name_id').val(key_id);
$('#txt_name').val(key_value);
return false;
})
});
This line only parsed before page load.
<div id="div_for_values">Edit</div>
The browser will only see this:
<div id="div_for_values">Edit</div>
You might want to use some ajax calls to insert fresh data into your fields.
I have a drop down list in my php page that is hyperlinked to different pages contained on different servers; html, asp.net pages etc.
The URL of these pages do not contain the same domain name as the drop down list. Therefore, in order to prevent the user from directly entering the URL in the address bar, I would like to post a variable when the user clicks an item in the list.
This variable will be sent along the hyperlinked URL. The asp.net page will check whether the variable is received along with the request, if yes the page will load otherwise the asp.net page will redirect the user to my home page.
My question: What is the process of posting a variable? Where in the hyperlink does this variable need to be included. I am totally lost.
EDIT: I tried to post through form.submit
<li><form id="sampleForm" name="sampleForm" method="post">
<input type="hidden" name="total" id="total" value="">
<a onclick="setValue();">ELMSTest</a>
</form></li>
<script type="text/javascript">
function setValue(){
document.sampleForm.total.value = 100;
document.forms["sampleForm"].submit();
}
</script>
I dont know how to proceed from here.
Try this as you are using the POST action. Which you are by virtue of having `method="post" on your tag.
`<form id="sampleForm" name="sampleForm" method="post">`
Because you have no action="script.php" it will send your inputs to the script that generated this page and run it again. So assuming this is called script.php...
In the PHP code of script.php the data will be delivered to you in the $_POST array.
<?php
if ( isset($_POST['total'] && $_POST['total'] == 1 ) {
// I have a foo variable with the value of one. So do something with it.
}
If you want to use the get action or add variables to a hyperlink as:
www.domain.net/script.php?foo=1&bar=2
The ? starts the list of variables, the querystring and each variable is seperated by an & if you want to pass more than one.
And of course the variables are passed in the $_GET array to your PHP code.
<?php
if ( isset($_GET['foo'] && $_GET['foo'] == 1 ) {
// I have a foo variable with the value of one. So do something with it.
}
if ( isset($_GET['bar'] && $_GET['bar'] == 2 ) {
// I have a bar variable with the value of two. So do something with it.
}