After searching for internet and even here for 5 hours, i am still stuck at getting value of a local variable in a function and sending it to PHP.
I have tried different syntaxes but none of them seems to be working. This code below, takes an input field from PHP and assign some value to it (having problem send this value back to PHP)
$(document).ready(function() {
//select all the a tag with name equal to modal
$('form[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
//Dont go until value is not 7
console.log('i am Now waiting for input');
var s = $('#serial').val();
console.log('searching for ' + s);
while(s.length != 7) return;
//When code value reaches 7
var code = $('#serial').val();
console.log('Value is reached ' + s);
});
});
In PHP
echo "<script>document.write(code);</script>";
Uncaught ReferenceError: code is not defined
please help
You should do somthing like this in javascript when $('#serial') is your input field.
$.ajax({'url':'your_php_file_url', 'type':'post', 'data':{'str':$('#serial').val()}}).done(function(data){
console.log(data.code);
});
And in php
$output = Array('code' => 'not set');
if (isset($_POST['str']))
{
//do your search and assigne value to output
$output['code'] = 'some value';
}
echo json_encode($output);
In short - you send your search string via ajax to php and make your searches. Then you echo it out as json, then ajax will read it as an object and you can use those values you want.
ok, i have almost tested every possible solution but all in vain... so i am off and try to use some normal method rather than javascript. Thanks for all your answers guys.
Related
Let me start by saying I am new to using jQuery so this may seem like a dumb question. When the vin textbox focuses out it trigers the jQuery script to run.
$("#vin").focusout(function() {
$("#stocknum").load("../jquery/update_stocknum.php?vin=" + $("#vin").val());
});
The PHP file is as follows and appears to be running correctly:
if(isset($_GET['vin'])){
$vin = $_GET['vin'];
if(strlen($vin) > 9) {
$stocknum = substr($vin,-6);
echo $stocknum;
}
What am i doing wrong so that I can set the value of the textbox to the what php is returning in the variable $stocknum?
Thanks in advance for your help!
Try -
$("#vin").focusout(function() {
$.get("../jquery/update_stocknum.php?vin=" + $("#vin").val(),function(data){
$("#stocknum").val(data);
});
});
I have a specific array that php needs to access and write to a file. I also want to be able to call the php to get the array info back. I use JSON.strigify to store the array in a string, but i cant figure out how to send it to a server with php. I have very little php experience and i tried:
<script language="javascript">
var COMMENTS_FOR_DISPLAY = new Array('Have fun with this code: Chris');
// Adds a new comment, name pair to the Array feeding textualizer.
function add_comment() {
// Retrieve values and add them to Array.
var new_comment = $('#kwote').val();
var new_name = $('#name').val();
COMMENTS_FOR_DISPLAY.push(new_comment + ': ' + new_name);
// Reset <input> fields.
$('#kwote').val('');
$('#name').val('');
var arrayAsString = JSON.stringify(COMMENTS_FOR_DISPLAY);
}
$(document).ready(function() {
var txt = $('#txtlzr'); // The container in which to render the list
var options = {
duration: 5, // Time (ms) each blurb will remain on screen
rearrangeDuration: 5, // Time a character takes to reach its position
effect: 'random', // Animation effect the characters use to appear
centered: true // Centers the text relative to its container
}
txt.textualizer(COMMENTS_FOR_DISPLAY); // textualize it!
txt.textualizer('start'); // start
});
</script>
in main.php i put:
<?php
$kwoteString = $_GET["arrayAsString"];
echo $kwoteString;
?>
I used echo to see if i was getting any output,but i wasn't. It could be a very simple fix, maybe im missing a header or something telling my html document to read main.php?? any help would be appreciated!
Use jquery with
$.post(url,params);
there are many tutorials around the web and stack overflow itself.
Here the doc:
http://api.jquery.com/jQuery.post/
you can add a hiddenField and set the string to the hidden field.
php code will read the value from hidden field.
I had a weird problem here. Please check the code below. I do not understand how this code below is created by somehow part of it is working, considering mixture of php variables and javascript variables.
First, let us see what the alert() outputs (take note that $lat and $lng is different from the array $vlat):
In Line 8 of the code below, alert(eventlocation) properly displays the coordinates of the GoogleMap latlng (so the implementation is correct)
In Line 13, alert(s) was able to display incrementing values (i.e. 0,1,2,3,4,..) based on the for loop in the previous line so it is also correct.
In Line 14, alert($vlat[0]) was able to display the latitude of the first element of that array (declared before this set of codes) so it is also correct
In Line 15, alert($vlat[1]) was able to display the latitude of the second element of that array so it is also correct
But in Line 16, alert($vlat[s]) displayed undefined.
Can anyone explain that? Thanks
echo "<script src= 'http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA2jrOUq9ti9oUIF0sJ8it1RTNr3PHi_gURF0qglVLyOcNVSrAsRRu2C3WQApcfD0eh9NLdzf9My0b9w' type='text/javascript'> </script>
<script language='javascript' type='text/javascript'>
function getabc(){
var eventlocation;
var volunteerlocation;
var s;
eventlocation = new GLatLng($lat, $lng);
//alert(eventlocation);
var volunteerDist = new Array();
s = $ctr;
var tvid = new Array();
for(s=0;s<$numrows;s++){
//alert(s);
//alert($vlat[0]);
//alert($vlat[1]);
//alert($vlat[s]);
}
}
a = getabc(); < /script>";
alert($vlat[0]);
alert($vlat[1]);
alert($vlat[s]);
$vlat[0], $vlat[1] and $vlat[s] are parsed on the server before they is sent to the client. The first two can be resolved, but PHP does not know what s is, since s is only defined once the client side is reached.
Edit from chat discussion
$json = json_encode($vlat);
echo "<script language='javascript' type='text/javascript'>
function test(){
var obj = JSON.parse('$json');
alert(obj);
}
< /script>";
The variable s that you are using to index the PHP array vlat is a variable that you declared in JavaScript. You cannot use it to index a PHP array.
What your code tries to do is to have PHP access a JavaScript variable, which it cannot. You can have PHP echo JavaScript, yes, but it doesn't work both ways.
I'm trying to post some data to a PHP file using jQuery. If I send the data via a form everything works just fine, but I want it to send in the background via jQuery. The click function works (except $.post), because I have tested it with an alert() and when I comment out the $.post line everything else works. If I don't comment out the $.post line the last two lines don't work.
Here is my javascript stored in admin.js:
$(document).ready(function(){
$(".admin-refresh").click(function () {
var movieID = $(this).prev().text();
$.post("actions.php", {refreshMovie: yes, movieID: movieID});
$(this).removeClass('btn-warning');
$(this).addClass('btn-success');
});
});
Here is some of the code from actions.php. This file is working if I post the data via form.
//Refresh Movie Details
if ($_POST['refreshMovie']) {
$movieID = $_POST['movieID'];
Here is the code from active-movies.php, which contains the button that activates the javascript.
<button class="btn admin-refresh"><i class="icon-refresh"></i> Refresh</button>
The files are stored as such ROOT/admin/active-movies.php, ROOT/admin/actions.php, and ROOT/includes/js/admin.js.
Thank you for any help you can offer!
At least one of your problems is here.
{refreshMovie: yes, movieID: movieID}
should be
{refreshMovie: "yes", movieID: movieID}
Try adding a third argument to the $.post() (a callback) like:
$.post("actions.php", {refreshMovie: yes, movieID: movieID}, function(response){
// actions.php should return some data to check if the
// action was successful
// that data will be available as a variable ("response")
if ( response == 'success' ) {
// do something
} else {
// do something else
}
});
Just enclose the parameters names into quotes, because the JS will make the mistake to think that movieID:movieID is like Kung Fu Panda:Kung Fu Panda.
{'refreshMovie': 'yes', 'movieID': movieID}
This Works fine and count returns 2:
$request_ids="166409183417843,1913616994605";
$sent = explode(',',$request_ids);
$count = count($sent);
But when using Jquery to post to another page sent var returns only the last id and count returns 1.
Page of origion:
$(function(){
$.post("process_ids.php", { request_ids: response.request_ids } );})
process_ids.php file:
$sent = explode(',', $_POST['request_ids']);
$count = count($sent);
I also checked with alert() the value of response.request_ids value and it's the same.
Something is totally screwed here, what's wrong?
I hate answering my own question but it's better than no answer.
The problem was like some mentioned above - the data type of the variable.
I simply cast the string to it before sending:
$(function(){
$.post("process_ids.php", { request_ids: String(response.request_ids) } );
return false;
});