This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm trying to accomplish a JavaScript form that will load the result from a PHP request on an external website, after a text box is filled out.
Right now, I have
<script type="text/javascript"/>
function sendrequest() {
document.location="myphpsite.com/submit.php?tb="+document.getElementById("tb").value;
}
</script>
<form>
<input type="text" id="tb" onChange="sendrequest()" />
</form>
This code is going nowhere, because from my current knowledge, you can only load PHP variables on startup, by using "script src=... ", and by having the php page return some javascript code.
What I'm looking for is a way to request/receive php variables dynamically, something that would work like this:
<script type="text/javascript">
function getdata() {
return document.get("myphpsite.com/submit.php?tb=hi");
}
</script>
In other words, is there a function that dynamically "reads" a website, and returns the raw text?
http://w3schools.com/ajax/default.asp
You should go read this and then read it again, AJAX is what you need.
best example with PHP and AJAX -
http://www.w3schools.com/php/php_ajax_database.asp
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I am trying to save the value I get from a drop-down list to a PHP variable. I am currently able to do so but through another page. However, I want to do more than just echoing the variable, I want to use it as a parameter for one of my PHP functions on the same page.
I've tried several other solutions I found online but all of them had a submit button, which I don't want. I just want the variable to change on the same page as soon as the drop-down value is selected.
I also tried replacing 'ClientAjax.php' with my current page 'Month.php' but all I get is the same page inside the '#result' div.
Dropdown menu: (Month.php)
Used to populate my dropdown with values from a database.
<select id="clientselect">
<option selected>ALL</option>
<?php echo getClients(); ?>
</select>
<div id="result"></div>
AJAX: (Month.php)
I post twice since I need to show the default selected value as soon as I refresh.
<script>
$(function(){
var displayclient=$("#clientselect option:selected").text();
$.post('ClientAjax.php',{postclient:displayclient},
function(data){
$("#result").html(data)
});
$("#clientselect").change(function (){
var displayclient=$("#clientselect option:selected").text();
$.post('ClientAjax.php',{postclient:displayclient},
function(data){
$("#result").html(data)
});
})
})
</script>
ClientAjax.php
<?php
$client = $_POST['postclient'];
?>
Before you can use $_SESSION variables you need to start a session. Use the PHP command 'session_start()' to do this, most likely on the page that will use the $_SESSION variable. Be sure to place the command at the very top of the PHP block.
Then in your AJAX php script you need to place this command after the variable value is set. $_SESSION["var_name"] = $variable;
Then in the function that will use the variable just replace whatever variable you are using now with $_SESSION["var_name"].
This will only work if you do a submit sometime after the AJAX call. The PHP function on the page will not run again until you submit. Maybe this approach is not for you?
Post back if you need any more help,
CharlesEF
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I have simple jQuery code where Im trying to like in PHP echo in jQuery to my website. But when I do it like this:
$( ".doc-list" ).html( "include 'documents.php';" );
My code echo it on site like:
include 'documents.php';
So my problem is when I make code like upwards jQuery echo it like plain text not like PHP and I need to include to my index.php documents.php when AJAX is done.
To load data from server use .load():
$( ".doc-list" ).load( "documents.php" );
Also, learn about client side and server side differences. Javascript (client-side) does not know anything about php code or any other code on your server side. Helpful question is here.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
How do I get values of HTML form value and pass it in PHP code inside javascript?
I need to call a PHP function inside a javascript.
Please kindly help me fix my code.
Thank you very much.
<script>
function Send() {
var abc="<?php echo mail_message.val(); //Email functionality here.. ?>";
document.write(abc);
}
</script>
<body>
<form>
<label class="email_label" for="mail_message">Message</label>
<textarea rows="7" cols="78" name="mail_message" id="mail_message">Enter your message here...</textarea>
<form>
</body>
PHP runs before your JavaScript gets loaded, so if you need to send data to PHP you need an AJAX request:
$.post('script.php', {
value: $('#mail_message').val()
}, function() {
// success?
});
Your script.php will then read $_POST['value'] and process it.
You can't use javascript val() in php syntex
instead use the following code to get the php value in javascript
<script>
function Send() {
var abc=document.getElementById('mail_message').value;
document.write(abc);
}
</script>
This question already has answers here:
Executing <script> injected by innerHTML after AJAX call
(12 answers)
Closed 9 years ago.
I'm writing an AJAX page with php, which will echo a block of JS code when it finishes.
Although the JS code is embedded into HTML page, it doesn't work. How can I make it work?
Browsers generally don't execute js in ajax sections, for security reasons.
You'll want to provide the final javascript to be executed in a callback to the ajax load functionality instead.
Even better, just include the javascript from the target page in an external initialization function (e.g. function finalizeProfilePage()) in some siteName.js file and then load that upon completion of the ajax load and where-ever else you use that page content.
I'm right now working on something similar.
To execute the code loaded through ajax i just need to call the function like this:
jQuery(function() {
loadScript();
});
(yes i'm using jquery )
This question already has answers here:
How to get JavaScript variable value in PHP
(10 answers)
Closed 9 years ago.
I just want to know if sending value of a javascript to php is possible.
here is the simple code of what i am trying to say.
<script language="javascript" >
var id = "data"
</script>
<?php
$getthevalueofid = var id;
?>
thanks!
Not the way your code sample is structured, no. PHP is a server-side language and Javascript is a client-side language. PHP is out of scope by the time any client-side script executes.
If you need to pass an object from the browser to your server you can use XMLHttpRequest (commonly referred to as AJAX).
To send a Javascript variable back to PHP, you need to do an AJAX request:
<script language="javascript" >
xmlhttp.open("GET","getvalue.php?id="+id,true);
xmlhttp.send();
</script>
And in getvalue.php you'd have:
<?php
$getthevalueofid = $_GET['id'];
?>
No way to do this. Use Ajax to pass javascript variable to php