How to alert box from ajax php function - javascript

I have function frm_trigger_entry_update this is the php function which is run in background i mean this is ajax php function.
In this function i have write some jquery or javascript function which will alert some text message.
In bellow snippet code you can see my function code.
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
}
?>
I have tried bellow snippet logic but its not work for me mean the alert box is not showing after calling this function.
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
?>
<script>
jQuery(document).ready(function($){
alert("my message");
});
</script>
<?php
}
?>
So how can alert the box in this php function any one have any idea.

Use JS and Php Separately.
First ajax call from your JS file:
$.ajax({url: "frm_trigger_entry_update function's Url",
success: function(result) {
alert(result);
}
});
In php Function, from where you should send your message:
function frm_trigger_entry_update($atts) {
echo "my message";
}

Consider following is your ajax call
$.ajax({url: "URL to frm_trigger_entry_update",
success: function(result)
{
alert(result);
}
});
Your PHP function
<?php
function frm_trigger_entry_update($atts)
{
echo "my message";
}
?>

Try this:
echo "<script>alert('test');</script>";

Note : The best practice to do this with Ajax because function is in
server side so you should call it to server from Client using Ajax
Create one file eg: "frm_trigger_entry_update.php"
IN "frm_trigger_entry_update.php" put your function
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
echo $atts;
}
// Call to that function
frm_trigger_entry_update("ATTRIBUTE_VALUE");
Write Ajax on your HTML
$.ajax({
type: 'get',
url: 'PATH to frm_trigger_entry_update.php',
success: function(data) {
alert(data);
}
});
You will get output Alert as ATTRIBUTE_VALUE

In your php function you need to return the output :
<?php
function frm_trigger_entry_update($atts)
{
return "<script>
jQuery(document).ready(function($){
alert('my message');
});
</script>";
}
And then where you want to apply this script you can display the output of your function :
<?php
...
$script = frm_trigger_entry_update($ats);
echo $script;
However in my opinion, this is not a good practice, you should put your javascript in a js function, in a js file and include your js file in your document or call it when needed.

Calling a php function via ajax is really impossible because ajax uses a url for php file and not a php function.
Though your code works and will alert if you call it via php.The code below will alert what ever string you put in the parameter of your function.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<?php
function frm_trigger_entry_update($atts)
{
//here i have some php code which run properly
?>
<script>
jQuery(document).ready(function($){
alert("<?php echo $atts;?>");
});
</script>
<?php
}
//calling the function and passing a simple string
frm_trigger_entry_update("Alert Message");
?>
</body>
</html>

Related

How to call a php function on a html text click?

I want to call php function while call that function I need to pass parameter, when click on some text, It has to call the method called test();
How to do this.
I don't know anything about ajax.I have taken this code to try it.To try I have used simple code it is also not working.
This is Ajax.html file
<!DOCTYPE html>
<html>
<head>
<body>
<p>Click Me</p>
<script>
$(document).ready(function() {
$("p").click(function(){
$.ajax({
url:"script.php", //the page containing php script
type: "POST", //request type
success:function(result){
alert(result);
}
});
});
})
</script>
</body>
</head>
</html>
script.php contains,
<?php
function test(){
echo "Hello";
}
?>
how to call php function when user click on some text.where I have to call that test() method. Can Any one help me?
Either you need to call when page called
<?php
test()//mind it
function test(){
echo "Hello";
}
?>
Or use any param like name of action
<?php
if(isset($_GET['action']) && $_GET['action']==1){
test()//mind it
}else{
otherFunc()//mind it
}
function test(){
echo "Hello";
}
?>
Set action in url
url:"script.php?action=1",

PHP: echo as condition of if statament?

I need to put echo as condition of if statement. So, for example:
<html>
...
...
<body>
<form onsubmit"<?php $c=false; if(echo "<script>example();</script>";){ $c=true; } echo "\""; if($c){ echo "action=\"./\"";} method="post">
...
</form>
...
...
<script>
function example()
{
alert("This is only an example");
if(..) return true;
return false;
}
</script>
</body>
</html>
So I need to create a function in JavaScript (this top is only an example, but, in realty, it is more complicated) and call it in the if statement.
Update
The code should be execute "action" of the form only if the Javascript function is true.
PHP runs in the server. JavaScript runs in the client. So php can't use a js variable or its function to server side action. What u can do is , u can call JS function from PHP like
echo "<script> myFunction() </script>";
But in the same same case, u can use php values for JS actions,
eg
<script>
var x = <?php echo $myVar ?>
if(x == true){
alert("yahooo");
}
</script>

Partial Update a list with AJAX

I have a list from the database and I want to implement edit functionality where in onclicking a table column, the column becomes editable and on clicking out of column, the value gets updated.
I have used AJAX for this purpose. My code is as under:
Page1.php
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function showEdit(editableObj)
{
$(editableObj).css("background","#FFF");
}
function saveToDB(editableObj,column,id)
{
$.ajax(
{
url: "page2.php",
type: "POST",
data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
success: function(data)
{
$(editableObj).css("background","#FDFDFD");
}
});
}
</script>
The column of my table is as under:
<td contenteditable="true" onBlur="saveToDB(this, 'exmid','<?php echo $p0; ?>')"
onClick="showEdit(this);"><?php echo $p3 ?>
Note: $p0 contains the serial no of row from mysql database table and $p3 contains the displayed text.
The code for page2.php is:
<?php
include_once 'includes/db_connect.php';
?>
<?php
$result = mysql_query("UPDATE examlist1 set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE sno=".$_POST["id"]);
?>
Problem:
When I click on the column it becomes editable. Using alert() inside saveToDB() I have checked that the function is called on clicking out of the column and also values of column and id are correct.
Then I tried the alert() function inside $.ajax and it was not called. I am not sure whether ajax is running or not. This is the first time I am trying to use ajax in a php code myself.
Please suggest what is the problem and what is the solution? The code is being implemented on a Linux based server hosted at Godaddy using PHP 5.4.
Also I would like to set the background color on fail. How to write it inside ajax block?
If you are getting the correct values when alerting.In your page2.php. Use mysqli instead of mysql and also use $connection object in mysqli_query().
<?php
include_once 'includes/db_connect.php';
$column=$_POST["column"];
$editval=$_POST["editval"];
$id=$_POST["id"];
$result = mysqli_query($connection,"UPDATE examlist1 SET $column='$editval' WHERE sno=$id");//$connection is database connection variable
if ($result)
{
echo json_encode(array('success'=>true));
}
else
{
echo json_encode(array('success'=>false));
}
?>
Here is Javascript: Try 100% works (Define what you want on if/else statement)
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function showEdit(editableObj)
{
$(editableObj).css("background","#FFF");
}
function saveToDB(editableObj,column,id)
{
$.ajax(
{
url: "page2.php",
type: "POST",
data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
success: function(data)
{
var res=eval(data);
//if success then
if (res.success)
{
//write JS code that you want after successful updation
$(editableObj).css("background","#FDFDFD"); //<- according to problem
}
//if fails then
else
{
//write JS code that you want after unsuccess
}
}
});
}
</script>

PHP file never called from jQuery

I have the following script in a HTML file that is called when the document is loaded:
<script>
$(document).ready(function(){
setInterval(function() {
$.get('check_session.php', function(data) {
alert('Load was performed.');
});
}, 5000);
});
</script>
The alert message will be called on the interval, but the PHP is not actually called because nothing is echoed.
PHP file: check_session.php
<?php
//check_session.php
session_start();
echo '<script language="javascript">';
echo 'alert("successful")';
echo '</script>';
echo $_SESSION['user_token'];
if(isset($_SESSION['user_token']))
{
echo '0'; //session not expired
}
else
{
echo '1'; //session expired
}
?>
Essentially, I am trying to call the PHP file, check_session.php, on a five second interval. This is one of my first times implementing jQuery, and after much research, I am still lost.
Any suggestions as to why the php file is not called are appreciated.
---UPDATE:
From Network tab:
Check data parameter
<script>
$(document).ready(function(){
setInterval(function() {
$.get('check_session.php', function(data) {
alert('Load was performed.');
console.log('data: '+data)
});
}, 5000);
});
</script>
the problem might be here: $.get needs URL and other optional params. you are simply giving filename. I think you should add appropriate path also. try it !!

How to echo javascript into current html page from external php file

Let's say I'm currently browsing mypage.html, which in its header has a link to the following js file:
<script language="JavaScript" type="text/javascript" src="jsfile.js"></script>
In jsfile.js, there's a function keyup() that is executed when the user types something into #searchbar, whose value is then stored in search = $(#searchbar).val();
I then pass this value on to search.php as follows:
$.post( "search.php", { searchval: search }, function(sentdata){
console.log(sentdata);
});
where the content of search.php reads:
<?php
if(isset($_POST[searchval])){
$search = $_POST[searchval];
echo "input value is $search";
echo "<script type='text/javascript'> alert('its working') </script> ";
}
?>
However, instead of an alert pop up (or anything else that would normally be executed in JS), the second echo simply prints " alert('its working') " into the console.
How can I modify search.php to allow it to inject actual js into myfile.html? Note that I've also tried wrapping the js code in tag.
Related question: why is it that when I omit console.log(sentdata), search.php does no longer echo anything into the console?
How I can modify search.php to allow it to inject actual js in myfile.html?
First of all, you need to modify your javascript file:
$.post( "search.php", { searchval: search }, function(sentdata){
eval(sentdata);
});
And no need for javascript tags, just echo a valid Javascript code:
if(isset($_POST[searchval])){
echo "alert('its working');";
}
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/eval
you can do this
$.post( "search.php", { searchval: search }, function(data){
alert(data); // this will alert the data which will print on search.php
});
and in the php file echo the data you want to print like
if(isset($_POST[searchval])){
echo 'its working. i got'.$_POST[searchval];
}

Categories