Jquery and PHP rating function problem - php

I know that I was already posted similar question but I just can't find the answer and figure it out how to solve this problem.
I'm trying to customize Jquery Star Rating plugin (link text) but I do not know what to do to show the message based on response of PHP script.
Jquery script successfully send rating data to PHP scripts that query the database and based on that echo message of proper or improper rating.
What should I add to an existing JS code so I can get echo from PHP and base on that write a message on some DIV beside rating star?
Jquery:
$('#gal').rating('gl.php?gal_no=<?=$gal_no;?>&id=<?=$id;?>', {maxvalue:10,increment:.5, curvalue: <?=$cur;?>});
Simplified PHP code:
$br=mysql_query("SELECT count(gal) as total FROM ...")
if ... {
echo '0';
}
else echo '1';
}
Jquery code successfully transmitted data to PHP script and when the PHP done with checking data echo the result ('1' or '0'). How can I get this PHP result back to Jquery and based on them write a message? Something like:
if(data=="1")
{
$("#error").show("fast").html('not correct').css({'background-color':'#F5F5F5','border-color' : '#F69'});
}else{
$("#error").show("fast").html('correct').css({'background-color' : '#FFF','border-color' : '#3b5998'});
}
If someone has an idea...I would appreciate it.

You would have to modify the source of the rating plug-in, as it does not provide any way for you to handle return values of your script. Read the documentation of jquery.post method in jquery, and then try to understand rating's code. Notice that when calling post method rating plugin doesn't provide a callback method (in other words it just doesn't care what your php script returns). You could try to modify the code in such way, that it allows you to register your own callback method.

Related

Using php variable from one isset to another

I have a page where on form submit from previous page a div with something like invoice is created. I send that data to server and populate the invoice for print/generate PDF etc.
if (isset($_POST['kreiraj'])) {
$emailuser = $_POST["na_email"];
}
On same page I create with JS a PDF from canvas of that invoice and send it via Ajax for posting into PHP mail script witch is also on same page.
if (isset($_POST['fileDataURI'])) {
$pdfdoc = $_POST['fileDataURI'];
};
This all works but I have problems accessing PHP variables from each other.
I have read about function global and $_GLOBLAS[varables] but nothing seems to work for me. Can someone point out a way to do it?
TO be exact i need to access $emailuser from example code above in if (isset($_POST['fileDataURI'])) php mailing script.

call php from ajax javascript

I have a PHP-based template website that has been heavily modified.
I am in the process of adding some RSS feeds, most of which are easy to "interpret" and display satisfactorily but one is in caps with "pre" formatting as well.
I want to modify the content. I look at all the mods I make as education and invariably am able to google satisfactory solutions to the problems I come across but, despite an earlier extensive programming background, without a thorough grounding in Javascript, Ajax, PHP, CSS and HTML, there are sometimes things that just frustrate the hell out of me.
All I want to do is pass a block of text from javascript code to PHP code, massage it and get the result back.
I am at a point in a ajax/jscript function where...
items[i].content
...contains the block of text that I want massaged and I have a piece of code that I got from here, I think, that ostensibly calls the PHP code...
function compute() {
var params="session=123";
$.post('wxspellch.php',params,function(data){
alert(data);//for testing if data is being fetched
var myObject = eval('(' + data + ')');
document.getElementById("result").value=myObject(addend_1,addend_2);
});
...and, unfortunately, it isn't documented so I don't have a clue what has to be customized. All I have done so far is enter the name of my PHP script.
The PHP script is written like this...
$pspell = pspell_new('en','american','','utf-8',PSPELL_FAST);
function spellCheckWord($word) {
global $pspell;
$autocorrect = TRUE;
// Take the string match from preg_replace_callback's array
$word = $word[0];
etc......
}
function spellCheck($string) {
return preg_replace_callback('/\b\w+\b/','spellCheckWord',$string);
}
echo spellCheck("...... the data .......")
The PHP tests out fine with hard-coded data.
I just need to know what further customizing I have to do to the javascript and php in order to facilitate the passing and recovery of the block of text.

Jquery trying to access a php array and check what's inside of it

So, I'm not sure if I can do this. If not, then any suggestions would be appreciated. Sorry in advance if the question is ridiculous...
I have an array that I created in php which is holding different user names populated off the database. When a user submits a form I want to use jQuery to check that the user name being submitted does not already exist in the array already created. I'm not quite sure how to do this. This is where I'm heading.
PHP section:
$existing_users = array();
$existing_users[] = $users; //this is reiterating in a while loop
HTML section:
<input type='text' name='user_name' id='user_name' />
jQuery section:
function checkAllFieldsForm() {
if (jQuery.inArray($('#user_name').val(),$existing_users) == -1) {
alert('no way this worked');
}
};
Not sure if maybe I should be using $.each instead or something else...
It seems like I would need to access the array $existing_users by an id but I haven't given it one. Do I need to give it a division id?
What you want to do is create this as a javascript array while still on the server side. I.e. have php output it as a javascript array (by looping over the array values and emitting them into a javascript array, or by outputting the array in JSON encoding). Then it will be available to javascript on the browser side, and all is well. PHP variables themselves are NOT available on the browser side, since PHP does not run there and was finished running before the server sent the web page.
Take a look at: Generating a JavaScript array from a PHP array.
You'll have to print the array into the javascript source, so the javascript can read it.
Should be like this:
var client_side_existing_users = <?php echo json_encode($existing_users); ?>;
if (jQuery.inArray($('#user_name').val(), client_side_existing_users) == -1) {
alert('no way this worked');
}
(I called it client_side_existing_users to make it very clear that the variable exists on the client side / in the browser, and has left the server-side world)
Keep in mind, the user will be able to see the contents of existing_users by looking at the page source. This could also make the page size massive if there are a ton of users. I would love to know why you're doing this, because there's probably a better way.

How to use a javascript function in a pure php page

I am looking for a way to include a .js function with in a straight php page. By straight php I mean there is no html included.
Explanation of process if you will.
I have a page where employees must swipe their ID badge for access to a computer.
The employee swipes the badge, the magnetic strip is read, and the data sting is sent to the db to get the access levels etc. This works great unless I get a bad card read. What I have is a .js file that pulls the ID and the issue date of the badge from the data string and validates it before going to the db. If it fails it errors.
At the error point I will ask them to swipe the card again etc...
So back to the top, can I use this .js file in a php file.
If not, can someone point me to a library or chart where I can find the comparison values for js and php. (.js - var s = ""; | .php $s = ""; etc...)
Can't you implement the validation logic in php:
<?php
function validateId($id = null) {
// your validation code goes here
if($id != null) {
// Code to be executed for a successful swipe
echo "success";
} else {
// Code to be executed for a failed swipe
echo "An epic failure occurred. Please swipe again";
}
}
I think your best bet would be to install a server-side JS engine and then run it using system.
A reasonable example is Narwhal. If you install that on your server, then you can do something like this from PHP:
system('js /path/to/my/js/file.js',$retval);
The only thing you may have to modify is any case where your JS interacts with the browser (i.e. document.writes, DOM manipulation), but Narwhal has equivalents for interacting with the CLI.

PHP work with JS Jquery

How do i make PHP work with JS?
I mean more like, i want to check if the user is logged in or not,
and if he is then it will:
$("#message").fadeIn("slow"); ..
How should i do this?
I have an idea maybe have a file that checks it in php, and then it echo out 1 or 0.
And then a script that checks if its getting 1 then do the message fade in.. But im not as so experienced to script that in JS
You cannot directly pass variables from Javascript to PHP because the PHP run on the server before it's sent to the client. But you can 'pass' variables from PHP to Javascript.
For example:
echo('<script type="text/javascript'> var phpvar = '.$variablefromphp.';</script>');
However, you can manipulate what javascript your browser will print. You can first check if the user is logged in in PHP, and based on that, conditionally print the HTML and Javascript.
For example
if($user->logged_in())
{
echo('<script type="text/javascript">$("#message").fadeIn("slow");</script>');
}
else
{
//php function
generateLoginBox();
}
I only javascript to enhance user experience. You should make your application work even when javascript turned off.
With the javascript enabled, you can add an enhanced experience, such as animated page element, AJAX request, etc.
In case of login state, you should have a way to know it in PHP script. Then in the output, you can have a conditional block that only executed if the login state is true. You can put anything you want here.
Javascript can be working in a static HTML page. You can use this to create a simple test for the code that you wrote, to see if it working as you want. Read the documentation in http://www.jquery.com/, there are many links there to many examples.

Categories