Access Input id from the include page in php - javascript

I am trying to fetch the 'id1' mentioned on the different file. I have included the file using the include function. But does not get success. Can you please let me know where i am making mistake. Or is there another way to access this field
The detail is there underbelow
I have this text field in one file (file1.php) <input id ="id1" type = text" value ="location">
I have another file (file2.php), where i include the above file as <?php include("file1.php"); ?>
How can i access the 'id1' using document.getElementbyId. in file2.php
When i use it directly as document.getElementbyId('id1').value; its not coming.. Please help

The problem is that Javascript is case sensitive, and you're trying to use a method that doesn't exist.
Your B should be uppercase: document.getElementById

Related

How can I pass smarty variable to .js file for var definition?

I have in php getting id from server request for parameter id
$id = $_REQUEST['id'];
$smarty->assign('id', $id);
And in the smarty .tpl display files {$id} outputs the number to the client browser.
I need this variable in the javascript for the page:
var id = {$id};
I use id later in the javascript to define datatables default search value.
"oSearch": {"sSearch": offer_id},
My problem is I need to properly pass the id to the javascript, if is set at all. Right now it displays {$id}. Any ideas, please?
In your template (.tpl) file, include the code
<script>
var id = {$id};
console.log(id); // for debugging purposes
</script>
In your browser's Javascript console, you should be able to see the value of id.

Wordpress get post id with a php file

I have been stuck on this for weeks. I have an HTML slide presentation using Reveal.js. I want to run a php script on the very last slide.
This is an HTML button on the last slide within a Wordpress post:
<button onclick="myFunction()">Open php file</button>
<script>
function myFunction() {
window.open("../../testing/test.php", "_self");
}
</script>
This is the code inside the test.php file that I am trying to run but it returns null:
define('WP_USE_THEMES', false);
require_once('../wp-load.php');
echo "Post ID: ".get_the_ID(); // Returns nulls but I want this to return 86.
The post id is 86. I can hard code it into the html (if I have to) but I don't want to hard code it into the php file. Also, I would prefer not to use jquery. How can I get the post id into the php file? Thanks.
Have you tried using global $post variable??
global $post;
And after that, you can get the id,
echo "Post ID: ".$post->
Another approach is using $wpdb global variable to make database queries.
You can also use JavaScript to send your post id to another php file using javaScript forms, Ajax or jQuery. jQuery Post.
Maybe the most easiest approach for you is something like this
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name'] after your javascript was executed.

Get filename in dropzone using jQuery

hy guys I try like this, my quetion is how I can grab filename or name..?
console.log(myDropzone.files);
myDropzone.files returns an array of File objects. You could access specific file properties using index.
Eg.
console.log(myDropzone.files[1].name);//Getting second file name
Go to dropzone.js and find
Dropzone.prototype._finished() function and alert this
responseText.success
you will get the file name

How to access session variable from JavaScript file (.js) file?

I have to access session variable from javascript file
I am using below code to access from javascript,
var sessionValue = '<%=Session["username"]%>'
alert(sessionValue)
it is working well when the script function is placed inside the aspx page...but when I use the same code in .js file it is not working
I have seen all the related answers, nothing is useful for me, so don't mark it as duplicate
i think try to write your session value to a hidden html element and read value of this hidden element with javascript as follow :
<input type="hidden" id="session" value="'<%=Session["username"]%>'">
at your js:
var sessionValue =document.getElementById("session").value;
Depends on what is your scripting languauge in server side,
if it is JSP or PHP following should work.
var sessionValue = "'"+<%=Session["username"]%>+"'"
alert(sessionValue)

How to update JavaScript variable using PHP

I have a JavaScript file named a pricing.js which contains this content in it:
var price_arr = new Array('$ 16.95','$ 30.95','$ 49.95','$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95');
But I want to to update this part of JavaScript file using PHP so please help me in this how can I update this part of the content from JavaScript file using PHP?
'$ 16.95','$ 30.95','$ 49.95','$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95'
Please understand that Javascript is Client Side code and PHP is server side code.
There can be 2 possible ways or scenarios which you want to achieve:
If you want to manipulate the JS file before sending it to client, you can rename the Javascript file to have .php extension and write php code to provide a variable. For Ex:
<?php
// write php code to create a string of values using a for loop
$price_array_string = "'$ 16.95','$ 30.95','$ 49.95',
'$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95'";
// Javscript Code var price_arr = new Array(`<?`php echo $price_array_string ?>);
The other alternative is that you get the value of price array by making an Ajax Request to a PHP web service.
Simply give the pricing.js file a .php extension. You can then include PHP in the javascript file the way you would for any other page. Just be sure your HTML code specifies that it's still "text/javascript" when you load it. You'll probably want to set the content-type in the PHP file as well, like so: header("Content-type: text/javascript");
Using your PHP script you can put a value in an HTML5 data attribute of an element on your page and then read it from your JavaScript, e.g. <body data-array="'$ 16.95','$ 30.95'"> can be generated with PHP, and then read with JS: var are = new Array($('body').attr('data-array').split(',')). Here jQuery is used in order to read the attribute value, but you can also do it with pure JavaScript.

Categories

Resources