How to pass the value from url of php to .js - javascript

Hi I am new to javascript and I am trying to pass a value from Get in url of php to javascript.
This is my code.
this is the url
someproject/index.php?roomId=2
index.php
room = $_GET['roomId'];
<input hidden id="room" value=<?=$room;?>>
script.js
I want to get the value from php and I want something like:
$('#room').getValueFromPHP();
but I do not know how to do it. Please help me.

This should do it:
PHP
<input type="hidden" id="room" value="<?=$room;?>">
JavaScript
$(function() {// make sure the document is ready.
var url = $("#room").val();
});

In order to pass a value from PHP to JavaScript, you can use echo as follows:
<?php $room = $_GET['roomId']; ?>
<script> var room = <?php echo "'$room'"; ?>; </script>

Related

Auto-fill form on WordPress

I am using the Scripts n Styles plugin with this code to auto-fill a logged in user's email into form on WordPress:
<script type="text/javascript">
window.onload = function () {
document.getElementsByName("Element-Name")[0].value = "<?php $current_user->user_email ?>";
}
</script>
However, when I refresh the page I get the following text (the quoted value in the code above) instead of the actual value.
<?php $current_user->user_email ?>
Any idea what I am missing here? Is this an issue with the plugin?
The first thing I see is that you don't output anything.
Try
<?php echo $current_user->user_email; ?>
But in your example it should have been just an empty value. There is something fishy with the PHP parser not detecting the PHP part.

JavaScript innerHtml not changing to php session

I am trying to change html button's text with php and javascript but it doesn't give errors and i don't know what is wrong.
My code that i am trying to get working.
At start of the file:
<?php
session_start();
$_SESSION["LOGGED"] = "Log in";
?>
Then comes the button code
<button class='button' id='login'>button</button>
<script type="text/javascript">
<?php
echo "var msg = '" .$_SESSION["LOGGED"] . "'";
?>
document.getElementById('login').innerHTML = msg;
</script>
What is wrong? The text just doesn't change.
Edit: The 2 sciprts php part crashes the script? It says unexpected token <. Removed the php part and putted to .innerHTML = "test"; and the button changed to test. So it should be somewhere at the php part?
If you're using the <button> tag,
document.getElementById('login').innerHTML = msg;
should work.
But if you're using <input type="button">, then you will have to use .value instead of .innerHTML
document.getElementById('login').value = msg;
EDIT: Try this...
<script>
var msg = <?php echo $_SESSION["LOGGED"]; ?>; //Don't forget the extra semicolon!
document.getElementById('login').innerHTML = msg;
</script>
Is your file a .php file and does the php work correctly?
try testing your php by putting
<?php echo "Hello world!"; ?>
at the top of your page :)

Replace a php variable with textbox input

Ok I am confused with php, javascript and html and dont know what to do. On researching on the internet, i found js is client side and php is server side. when a php file is run on the browser, it converts everything into html and the page is loaded. Now let me tell you guys what i am doing.
I have a php file that give me some stats from a particular url (in the sample i am just showing url)
<?
$url="www.example.com";
echo "URL = " .$url;
?>
Result URL = www.example.com
The above code echoes the url which is www.example.com. I added a textbox to this code which i believe is javascript+html
<script>
function myFunction() {
$url=myurl.value;
}
</script>
<input type="text" name="myurl" id="myurl">
<input onclick="myFunction()" type="submit" name="btnurl" id="btnurl" value="Submit">
<br><br>
<?
$url="www.example.com";
echo "URL = " .$url;
?>
Here the result is same. only difference is that it has a textbox and button above the result.
When I enter another url in the textbox and press submit, it does nothing probably because the page is already loaded. I want to replace the result of www.example.com to the one which is entered in the textbox without changing the .php file. There will always be a default url in the .php file. whenever the file is opened in the browser, the default statistics will be shown... only when the user enters new url and clicks submit, the stats should change.
How can I achieve this? I am behind this since more than a couple of hours now and not sure how to get this done. Please help me.... Thank you.
EDIT
Can I have two .php files? one for the user to enter url and submit and another one to get the entered url and echo it? If yes, how? If I understand this logic, i can get a start for what I am doing.
I think you are trying to do more with your js function, but syntactically it is combining js and php. It should look like this
function myFunction() {
var url = document.getElementById('myurl').value;
}
Although this doesn't really do anything other then assign the content of the text box to a variable.
EDIT
<script>
function myFunction() {
document.getElementById('url').innerHTML = document.getElementById('myurl').value;
}
</script>
<input type="text" name="myurl" id="myurl">
<input onclick="myFunction()" type="submit" name="btnurl" id="btnurl" value="Submit">
<br><br>
<? $url = "www.example.com"; ?>
URL = <span id="url"><?= $url; ?></span>
natzim is correct if you are wanting to write the url back to the php file. If you use javascript to change the action of the form, it will submit to a different page.
//javascript
function myFunction() {
//this should change the page that loads after submit.
//If you want to go to a new page that the user enters, leave this code in...
//If not, remove it
document.getElementsByTagName("form")[0].action = document.getElementById("myUrl").value;
}
That is assuming you have a form tag somewhere (which you will need to submit the page). Also I am not sure this code will run if you use a submit and not a button. If you used a button instead you could append this to the code above to submit the form:
//This would be part of your myFunction if you used a button instead of a submit input
document.getElementsByTagName("form")[0].submit();
as per my comment -
this code is your old php:
<?
$url="www.example.com";
echo "URL = " .$url;
?>
and this is the php I suggested:
<?php
$url=isset($_POST['myurl']) ? $_POST['myurl'] : 'www.example.com';
echo "URL = " .$url;
?>
this would check the myurl input from that was submitted to the server and set the value of $url to its value if it existed then the $url variable would be echoed to the page under the inputs.
This code is assuming you are using the POST method rather than the GET method when your form was submitted.
**EDIT: **
To clarify - here is your page with the modifications I am suggesting. (Please ignore the javascript above as it seems you will not need it):
<form action='www.example.com' method='post'>
<input type="text" name="myurl" id="myurl">
<input type="submit" name="btnurl" id="btnurl" value="Submit">
<br><br>
<?php
$url=isset($_POST['myurl']) ? $_POST['myurl'] : 'www.example.com';
echo "URL = " .$url;
?>
</form>

Posting a javascript variable into a text area using PHP

I am using PHP to post into a textarea:
<textarea name="">
<?php echo $name?>
</textarea>
I have tried to do this.
Jquery:
var yo = "Hello"
$.post(window.location, {variable: yo});
PHP:
<?php
$name = $_POST['variable'];
?>
Why does this not write "Hello" in the textarea / Why is the php not receiving the .post?
can you try <?php $_SERVER['PHP_SELF']); ?> as URL at your jQuery post function instead of window.location as follow
$.post(<?php $_SERVER['PHP_SELF']); ?>, {variable: yo});
Assuming you want to change a value inside a textarea with a value you calculate or set in javascript, you can assign it this way using JQuery:
<script>
var yo = "hello";
$("#test").html(yo); //where test is the ID from the textarea
</script>
<textarea id="test">
</textarea>
FIDDLE: http://jsfiddle.net/YcgsR/
You can modify your logic a bit here by making your PHP script take in your JS variable and do some logic/manipulation/query/whatever (that you can't do via JS) and return a value which you want to appear in the textarea. You can then leverage this return value via an AJAX request like so:
var yo = "Hello";
$.post(window.location, {variable: yo}, function(value) {
$('textarea').val(value);
});
and in your PHP script, something like:
<?php
$name = $_POST['variable'];
/*
* do manipulation here that you can't do purely via JS
* otherwise why would you be posting to a PHP script?
*/
echo $some_return_value;
?>

Ajax form not getting a reply from php file

This form is working perfectly fine, e.g. it stops the form submitting, there's no javascript errors in the console log. However, when I press submit, I get an alert with nothing in it. I have searched the internet for multiple solutions and they all give the same for me.
Here's my Javascript/Html
<script>
$(function(){
$("#submit").click(function(event){
event.preventDefault();
$.ajax({
type:"post",
url:"templates/process-token.php",
data:$("#form").serialize(),
success:function(response){
alert(response);
}
});
});
});
</script>
<form id="form" method = "post" action = "security_check">
<input type = "text" name = "fld1" id = "fld1" />
<input type = "text" name = "result1" id = "result1" value = "value_from_php_page" disabled />
<input type="submit" value="Submit" id="submit">
</form>
And here's the PHP, it's so simple but doesn't work:
<?php
echo $_POST["fld1"];
?>
Any help here would be appreciated. Thank you.
Change data from $('#form').serialize() to a one test variable so you can see if it is JS or PHP that is causing the issue.
On your PHP page:
<?php
isset($_POST["fld1"]) ? $myvar1 = $_POST["fld1"] : $myvar1 = 'No data passed...';
echo $myvar1;
?>
This confirms that there is infact an fld1 in the $_POST, and if not, says so, taking away the guess work...
You could do this with JSON both ways. Here's how I would implement this kind of thing in JavaScript and PHP.
There's a great function in jQuery that is a shortcut for getting JSON data. It's a shortcut for $.ajax specifically for simple calls.
$.getJSON( "templates/process-token.php?"+$("#form").serialize(), function( data ) { alert( data.reponse ) } );
PHP could return a json encoded string so it can be accessed from the JavaScript code more easily.
<?php echo json_encode( array('response'=>$_POST['fld1']) ); ?>
That can happen only if the text box #fld1 is empty. Type something in the text box and it will work.

Categories