I need to assign javascript client Date (examp. - 2012/02/03 16:00:00) to php variable. Any idea how? I was trying to use this lines and changing them in million different ways. But I just cant get it.
Today = new Date();
var date = ????
var date = "<?= $date ?>";
I solved it this way:
<input id="date" type="hidden" name="date">
<script type="text/javascript">
document.getElementById('date').value = Date();
</script>
But thank you very much all.
Add an input in your form
<input type="hidden" name="clientDate">
if you are using jquery add this to set the client date input when the user submits the form
$(YOUR_FORM_SELECTOR).on("submit", function() {
$("[name=clientDate]").val(new Date());
});
If you want to go with vanilla javascript follow this answer
you can't simply assign a javascript variable to a php variable. php runs on the serverside and is executed before javascript.
you can however submit an ajax call with the value of your javascript variable to a php script.
you might wanna have a look at jquery's post function.
$.post("test.php", { yourDate: date } );
in your PHP script you'll be able to access the date with $_POST['yourDate']
you can also use a form and a hidden field as you say in your comment.
in this case you can use (assuming you're using jQuery)
$('#id_of_input').val(date);
This will convert js variable to php variable and php variable to js variable
<script>
function jstophp(){
var javavar=document.getElementById("text").value;
document.getElementById("rslt").innerHTML="<?php
$phpvar='"+javavar+"';
echo $phpvar;?>";
}
function phptojs(){
var javavar2 = "<?php
$phpvar2="I am php variable value";
echo $phpvar2;
?>";
alert(javavar2);
}
</script>
<body>
<div id="rslt">
</div>
<input type="text" id="text" />
<button onClick="jstophp()" >Convert js to php</button>
<button onClick="phptojs()">Convert php to js</button>
PHP variable will appear here:
<div id="rslt2">
</div>
</body>
Demo: http://ibence.com/new.php
Related
I'm a JQUERY begginner. I'm trying to pass a PHP variable to a js file so that i can use it in JQUERY! But found no resource to learn to do so. I'm able to pass a JQUERY variable to a different php file but I couldn't pass PHP variable to a Jquery file. Is really possible to do so?
function.php:
<?php $variable = $fetch['username']; ?>
app.js : How can use this variable in app.js file below?
$(document).ready(function(){
var flwbtn = $(".findrowpplfollowbtn");
flwbtn.click(function(){
var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();
$.post("../php/flw.php",{flwusernameselected:selectflwusername})
});
});
Echo it to HTML temaplate. If it's some structure/array use for example json format.
<script>
<?php $variable = $fetch['username']; ?>
var js_variable = <?php echo json_encode($variable); ?>
</script>
or
echo some hidden element like
<input type="hidden" id="custId" name="custId" value="<?php echo($fetch['username']); ?>">
and grab if with Jquery like
<script>
$( "#custId" ).value();
</script>
with your code
JS:
$(document).ready(function(){ var flwbtn = $(".findrowpplfollowbtn");
flwbtn.click(function(){
var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();
$.post("../php/flw.php",{$( "#custId" ).value()})
});
});
I tried to create a timer based on the user input for how long the timer will go
here is my code for user input userinput.html
<form method="post" action="update.php">
<input type="number" name="inputnumber" />
<input type="submit" value="OK" />
</form>
here is my code using javascript and php update.php
<script type="text/javascript">
function countdown(secs, elem){
var element = document.getElementById(elem);
element.innerHTML = "Please wait for "+secs+" seconds";
secs--;
var timer = setTimeout('countdown('+secs+',"'+elem+'")', 1000);
}
</script>
</head>
<body>
<div id="status"></div>
<script type="text/javascript">
countdown("<?php $test = $_POST['inputnumber'];?>","status");
</script>
</body>
I'm trying to pass the user input using php to javascript which is from this line
<script type="text/javascript">
countdown("<?php $test = $_POST['inputnumber'];?>","status");
</script>
I want the timer start based on the user input
but the result is the timer always start from 0
is my code wrong to passing the value from php to javascript ??
anyone know how to pass the value??
thanks
You need to echo the post parameter, otherwise it won't print for your JavaScript to use
countdown("<?php echo $_POST['inputnumber'];?>","status");
Or shorthand
countdown("<?=$_POST['inputnumber'];?>","status");
<a href='new.php?logi_jo=$_POST[jo]'>submit</a>
<input type='text' style='width:50px' name='logi_jo'>
is there a way of getting the the value of logi_jo and pass it to another web page? w/o using <form>
the first line of code is the link. and the the 2nd line of code is the textbox.. and then the value of that textbox will pass to another page w/o using form action. I decide to not use form action="??" because that link is inside of a form.. thank you for reading
you can do this simple task via javascript, ajax isn't necessary.
Your HTML
<a href='javascript:void(0)' onclick='submitMe()'>submit</a>
<input type='text' style='width:50px' name='logi_jo' id='logi_jo'>
Your javascript
<script type="text/javascript">
function submitMe(){
var val=document.getElementById("logi_jo").value;
document.location.href="new.php?logi_jo="+val;
}
</script>
in news.php page fetch variable by $_GET["logi_jo"]
AJAX to the rescue!
var logi_jo = $('input[name=logi_jo]');
$.post(
'test.php',
{ logi_jo: logi_jo },
function(data) { // do something to the DOM }
);
i suggest to begin with this:
http://www.w3schools.com/jsref/obj_location.asp
I'm using a form plugin / addon where I don't have control over how the form is generated.
The form generates HTML which looks like this:
<form>
<label>First Name</label><input name="Question25" type="text" value="" />
<input class="formBlockSubmitButton" name="add" type="submit" value="Submit" />
</form>
I would like to prefill some of the form values but I can't write, say
<input name="Question25" type="text" value="<?php echo $my_value; ?>" />
Because the form addon generates that HTML. I can write PHP or Javascript before this block, is there a way to search for an input with a name of "Question25" and then prefill it when the block comes up?
You could use jQuery and the .val() method:
<script type="text/javascript">
$(function() {
$('input[name="Question25"]').val('some value');
});
</script>
and if the value must come from the server:
<script type="text/javascript">
$(function() {
var obj = <?php echo json_encode(array('value' => $my_value)); ?>;
$('input[name="Question25"]').val(obj.value);
});
</script>
You can emit all the values you'd like to search as an array of fieldname/values, and then emit a function that tries to map those values to those fields.
<script type="text/javascript">
var values = [{fieldName:'Question25', value:'MyValue'}]; // iterate through your values in PHP and generate the {fieldName:'value',value:'value'} object
$(function() {
$.each(values, function(index, item) {
$('input[name=' + item.fieldName + ']').val(item.value);
});
});
</script>
var value='<?php echo $my_value; ?>'
$("input:text[name='Question25']").val(value);
use something like this
Use Javascript. With jQuery, you could do something along the lines of:
$("input[name=Question25]").val(my_value);
You can then insert PHP vairables into the Javascript with echo, load them with AJAX, or whatever you need.
Is it possible ?
Here is example :
<script>
var id=$(this).val();
$('#region').append(< ? php get_region(#####) ?>);
</script>
I need to call "var id" instead of #####
If you know what the id of "this" is going to be before the page is rendered on the browser (and THEN the javascript code will be run), then you can do the following:
<script>
<?php $id = $whatever_the_id_is; ?>
$('#region').append(<?php get_region($id); ?>);
</script>
If you don't know what the id will be of "this" before rendering the page, then you need to use ajax to run the get_region() function server side and return the string you're looking for.
in my understanding, php is loaded first on the server, and then goes to client side ex. javascript (which includes jquery). You can obtain a copy of php variable in client side very easily, by either embedding a hidden variable. ex.
<input type="hidden" value="<?php echo $a; ?>" />
or
<a title="<?php echo $a; ?>" />
You can also do this,
<script language ="javascript"> var global_variable="<?php echo $a; ?>"; </script>
However, php function can not refer to javascript variable.