Get a javascript variable to a php one - javascript

I have this code:
<script type="text/javascript" charset="utf-8" src="http://web.itoday.gr/load.js"</script>
<p><script type="text/javascript"> datesmart(0); namesprefix(0); names(0); </script></p>
<p><script type="text/javascript"> datesmart(1); namesprefix(1); names(1); </script></p>
I want to get the variable names(1) to a php variable. How will i be able to do this ?
Thanks a lot !

You cannot directly access JS variables in PHP. You have two options
1) Set the value of names(1) to a html element and access the value from that html element (like hidden field, span etc)
2) Use AJAX
As an example:
Add hidden field in the case you are submitting the value to another page using form.
<input type="hidden" id="nameval">
Instead of hidden field, use elements like span or div to display the value of the names(1) in the html page like,
<span id="nameval"></span>
Set the value of this hidden input field (and html in the case of span/div) using jquery. Call this function in appropriately depending on your need. Firstly, you need to include the jquery library as shown below.
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() // when the document is ready
{
$("#nameval").val(names(1)); // set the value of element with id nameval if you are using hidden field
$("#nameval").html(names(1)); // use this in the case of span/div
});
</script>

PHP runs on server and javascript in browser . You need to send a ajax request to access value from javascript.
On script load send a ajax request with data to the php script you want to access this variable.

Related

Using PHP to get the value of input type range onchange

I am trying to get the value of a range slider after it moves and then update my page. I have approached this by using "onchange" and calling some javascript to set a value to a text box and using php to get that value. The php does not even grab the value from the text area on load and I am not sure why. It says the id of the input text box is an "unidentified index." There might be a simple thing wrong or I may be approaching it completely wrong. I am new to this...
Here is the code for the slider.
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script>
function printValue(sliderID, textbox) {
var x = document.getElementById(textbox);
var y = document.getElementById(sliderID);
x.value = y.value;
}
window.onload = function() { printValue('slider1', 'rangeValue1')};
</script>
</head>
<body>
<form action='slider.php' method='get'>
<input id="slider1" type="range" min="100" max="500" step="10" onchange="printValue('slider1','rangeValue1')">
<input id="rangeValue1" type="text" size="2">
</form>
<?php
echo $_GET['#rangeValue1'];
?>
</body>
</html>
The js function does set input text box, but the php script doesn't happen. I need to get the value in PHP because the page I'm including it in is written in PHP. I don't know if, or how, the page has to be reloaded. I tried using location.reload() in the onchange function and it just continuously loaded the page.. Please help! Any input will be helpful! Thanks!
It looks like you might be getting Javascript and PHP mixed up.
PHP is run solely on your server when a browser accesses a php file. The output of the php file (like when you use echo) is sent as a webpage. However, Javascript is run solely in the browser. To make them communicate, you will need to load another webpage (or reload the current webpage). You can either use a form or directly craft the URL (probably easier in this case).
So you could do something like this inside printValue():
location.querystring="?value=" + x.value;
This will create a GET argument, which you can access with $_GET['value'], and reload the page.
EDIT: Performance Warning!
Every time the slider is moved, your server will end up resending the webpage, which could slow down the server. You might want to only send the new value after the user has clicked a button or something, in which case it would be easier to use a form.

Wordpress, PHP, Javascript, how to get variable within a js function?

Purpose: passing html hidden input variable into javascript function.
Working on a wordpress plugin and got stuck with javascript.
Here's hidden input I am trying to get, which is variable_product_id. This gets set when an user selects an dropdown options dynamically.
<form class="hello">
<input type="hidden" name="variation_id" value="">
</form>
Outside of form class hello, there's plugin function in below, and I am trying to get and set "variation_id" right after product_id within "wp.CheckoutButton.apply".
if( !is_user_logged_in() )
{
echo "<script type=\"text/javascript\" >
var temp = document.getElementsByName('variation_id');
//<![CDATA[
wp.CheckoutButton.apply({
BUY_BUTTON_LINK_URL:\"www.website.com/?p=42&product_id=\"+temp,
\"\":\"\" });
//]]>
</script>";
}
"wp.CheckoutButton.apply" prints button on the screen which will pass product_id that I am passing.
It's been working with no variable product options within woocommerce, for variable product, I have to get the selected hidden variation_id when an users changes values(dropdown input).
Can I use document.getElementsByName('variation_id');?
If so, how can I pass 'variation_id' within "wp.CheckoutButton.apply" function?
Is "+temp" within apply function legal?
A possible duplicate of this :
php variable into javascript
Sometimes, some data is needed in js files / js code like the base url of the site. In this case, it is best practice to create a JS variable within the header html tag. In your header tags, create a variable like below :
<script>
var baseUrl = <?php echo get_site_url(); //if get_site_url echos the result itself, then no need to use echo ?>
</script>
Now, in all of your JS files and inline js code you will have this variable and you can use it. Simply it is a global js variable (i just made it up, dont know if js has :P )
Now you can use that variable every where in your js files and js codes.
Notge : Please create baseUrl variable at top most of the header tag or some where, so that no js file is included before it.
**Edit : **
Now to get variation_id from your form element, add id to your element as below:
<input type="text" name="variation_id" id="variation_id" value="" />
In js using jquery :
var variation_id = $("#variation_id").val();
and in wp.CheckoutButton.apply , instead of temp, use variation_id. It is correct i think, but try it.
Hope this will help.
For that you need to create a localize script that can help to call a site url in js file or js function.
http://codex.wordpress.org/Function_Reference/wp_localize_script

Unable to call a function while using master page in Javascript at checkbox checked change event

I was trying to call a function when the checkbox status changes, it calls without a masterpage. I have tried the following code:
<script type="text/javascript">
$(function () {
$('#cbOption1').on('change', PublishToPreferredZerker);
});
function PublishToPreferredZerker() {}
</script>
[...]
<asp:CheckBox ID="cbOption1" runat="server" style="text-align: left"
Text="Publish the job to particular Zerker or a group of the Zerkers." /><br />
The function is not called when using MasterPage.
Note cbOption1 is not the client ID, but the ID for the server side.
You need to do something like (Use Control.ClientID Property to get the id for HTML element):
$('#<%=cbOption1.ClientID%>').on('change', PublishToPreferredZerker);
The code
$('#cbOption1').on('change', PublishToPreferredZerker);
finds the control with id cbOption1 and then acts on that control
But when using this code on a master page, the control id does not remain cbOption1.
It is prefixed by master page content Id.
something like ct$001cbOption1
To make it work when using master pages use code like this to find the clientId for the control :
$(#"<%= cbOption1.ClientID %>").on( .... )
I got success while I added below code on Page load function.
cbOption1.Attributes.Add("onChange", "javascript:PublishToPreferredZerker()");
Javascript function
function PublishToPreferredZerker() {}
Also I have tried above answers but not get required output.
Thanks,

How to pass data using submit() in javascript?

I want to post data from one page to another using javascript post method?
Below is the javascript I am using..
In test1.asp page
<script type="text/javascript">
function Service_Add(Data_ID,Data_Type)
{
var Data_ID=Data_ID;
var Data_Type=Data_Type;
document.miformulario.submit();// Here I want to pass data like "Submit(Data_ID,Data_Type)"
}
</script>
I want to post "Data_ID" and "Data_Type" to test2.asp page
To pass data when you submit a form, you have to include that data in a form input field (hidden, visible, doesn't matter).
You can add hidden fields in your HTML form like this
<input type="hidden" id="Data_ID">
<input type="hidden" id="Data_Type">
and then set the values in your javascript function and then submit (?)
<script type="text/javascript">
function Service_Add(Data_ID,Data_Type)
{
document.getElelementByID("Data_ID").value=Data_ID;
document.getElelementByID("Data_Type").value=Data_Type;
document.miformulario.submit();
}
</script>

how to refer a textbox inside body of html page in a javascript if textbox is not part of any form?

i have coded a header file which contains a input textbox for search.this header is include in body tag of a jsp page as shown below.
<body>
<include src="header.ssi">
<form>
</form>
onclick of button i header file i am running a javascript function.
i need to refer to this textbox in javascript function.
document.getElementById — but it should be in a form
If you can use jQuery then simply use $("#textboxid") to access the textbox

Categories

Resources