I'm trying to simply POST something using JQuery. when the element is clicked, I want the page redirect to the php file and echo out the POST value.
Html file
<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div onclick="posttest();" >hi there</div>
<script>
function posttest(){
var txt="hi";
$.post("testpost.php", {testVar: txt},function(){
window.location.href = "testpostphp.php";
});
}
</script>
</html>
PHP file
<?php
var_dump($POST["testVar"]);
?>
There is $_POST variables in PHP, not $POST.
Try this:
$.post("testpost.php", {testVar: txt}, function(response){
console.log(response);
});
Related
I have this code on Wordpress post:
<input type=text id="testni" value="la">
and this code in functions.php:
<script type="text/javascript">
$function(){
$("#testni").attr("value", "petra");
}
</script>
This does not work. I guess i have to add some php code?
How to call JS from php?
What you want is this
<script type="text/javascript">
$(function(){
$("#testni").val("petra");
});
</script>
Read about DOM ready
Read about setting an input value
If you need to call js in wordpress.. You need to add action in functions.php file. Try the below method.
<?php
add_action('wp_footer','custom_script');
function custom_script(){
echo "<script type='text/javascript'>
$function(){
$('#testni').attr('value', 'petra');
}
</script>";
}
?>
Or else simply add your script in header.php or footer.php.
I would like to export a variable from a php file in to a html file.
The php file is called example.php and looks like this:
<?php
$array= ['one','two'];
?>
The html file is called example2.html and want the $array variable to be the input of var list, like this:
<html>
<head>
<script type="text/javascript">
var list = $array
echo list;
</script>
</head>
</html>
I am new on this field and donĀ“t know where to start from, thanks for your help.
You could try json_encode() or implode() for printing your array from to javascript (html), i have shown you two solution below
<html>
<head>
<script type="text/javascript">
var list = <?="['".implode("','",$array)."']";?>;
//or
var list2 = <?=json_encode($array);?>;
</script>
</head>
</html>
you've used echo in your javascript code that is a syntax error, in javascript you can use alert() or console.log for testing your varriable, for example in top code you can try this: alert(list[0]);
How to call customs.js file in for loop and i want pass some argument with this js,And how to retrieve this var in JavaScript file.
for($i=0;$i<5;$i++)
{
<script type="text/JavaScript" src="http://localhost/customs.js?var=test'">
}
A better way to do this would be to include your custom.js in the html of the webpage and wrap it's code in a function to which you can pass a variable, like so:
<html>
<head>
<script type="text/JavaScript" src="http://localhost/customs.js"></script>
</head>
<body>
<!-- Your HTML -->
<script>
<?php
for($i=0; $i<5; $i++){
echo "custom(".$myvar.");";
}
?>
</script>
</body>
</html>
Then in your custom.js file you can do this:
function custom(input){
//Do something
}
I should note that you should be careful inserting unsanitized PHP into html directly. For more info, see: How to prevent XSS with HTML/PHP?
I'm trying to run a php script within a javascript <script> in a .php file. Is this possible?
<script type="text/javascript">
$("#id").example("
<?php print('Hello World'); ?>
");
</script>
No, that's not possible. JavaScript is (unless you use node.JS) running in you WebBrowser. php get's started by your apache webserver when the webserver gets an http request for example to http://localhost/test.php. So these two scripts run in two completely different scopes. If you want to start a php file (called REST-Service) you need to perform an ajax-request:
JQuery style: (I don't like it but anyway...)
$.ajax({url:"myTestFile.php",success:function(result){
$("#id").html(result);
}});
Best regards
Dustin
This examples shows you how you can pass $variable from php to Javascript
<?php $x = 'hello world'; ?>
<script type="text/javascript">
var str = '<?php print("Hello World"); ?>' ;
$("#id").html(str);
</script>
Instead of print, you can use echo and it works:
<script type="text/javascript">
$("#id").example("
<?php echo 'Hello World'; ?>
");
</script>
It Will help you.You can add php code in single quote. Try this code.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<div id="id">
</div>
<script type="text/javascript">
$("#id").text('<?="Hello World"?>');
</script>
</body>
</html>
I have a <div id="content">. I want to load the content from http://vietduc24h.com into my div:
<html>
<head>
<script type="text/javascript">
$(document).ready(function()
{
$("#content").attr("src","http://vietduc24h.com");
})
</script>
</head>
<body>
<div id="content"></div>
</body>
</html
I don't want to use an iframe. How can I do this?
You need to think about CORS in this aspect. The code you need to have is:
<script type="text/javascript">
$(document).ready(function()
{
$("#content").load("http://vietduc24h.com");
})
</script>
When your domain is not inside vietduc24h.com, you might get some security exception. In order to avoid that, you can host a local proxy here. In PHP, we do this way (url.php):
<?php
$url = file_get_contents(urlencode($_GET["url"]));
echo $url;
?>
And in the script, you need to modify this way:
<script type="text/javascript">
$(document).ready(function()
{
$("#content").load("proxy.php?url=http://vietduc24h.com");
})
</script>
Try this code with the jQuery Load function:
$('#content').load('http://vietduc24h.com', function() {
alert('Load was performed.');
});
If you encounter in security issues because of the Cross-Origin-Resource-Sharing policy than you have to use a proxy in your server code.
Try this:
$("#content").html('<object data="http://vietduc24h.com">');
Taken from this answer.