Trying to Send data using jquery .post() - php

In insert.php I have the following
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".like").click(function() {
var data = $(this).attr('data-id');
$.post("data.php", {'name': data}, function(response){
$('#dv').html(response);
});
});
});
// ...
</script>
<!-- ... -->
<?
if (!is_bool($result) && !is_null($result)){
while($row = mysql_fetch_array($result))
{
?>
<? $id = $row['uniqueid'];?>
<? echo "<br>ID: " . $row['uniqueid'];?>
<? echo "<br>Name: " . $row['surname']; ?>
<? echo "<br><a href class=like id=buton data-id='$id'> Like (" . $row['likes'] . ") </a>"; ?>
<? echo "<br><br>"; ?>
<!-- ... -->
in data.php I have
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
<script type="text/javascript" src="includes/js/jquery.form.js"></script>
<script type="text/javascript">
alert("<? echo $_POST['name']; ?>");
</script>
I fixed a couple of things I was told were wrong but it still does't work. The alert box is blank. Please help. Thanks.

this is completely wrong... for your data.php this will not parse javascript the way you are expecting.. it will simply take in POST data and ECHO out a result.. dont try and use client-side javascript because it will not do anything
data.php: its php just simply do
<?php
echo $_POST['name'];
?>
in your js if you really want a popup to show then you have to wait for the results of the POST/RESPONSE and do it
$.post("data.php", {'name': data}, function(response){
$('#dv').html(response);
alert(response);
});
remember the php script you call from a POST or ajax call will only process server side code.. PHP, perl, java, C... not client-side code like javascript

Related

Loading data from database in every few seconds without page refresh

I want to load contents from database in every 10 seconds to a particular . But however i try whole page gets refreshed. Not getting where oi am going wrong.
Here is my code
<?php
include('header.php');
include('includes/Connection.php');
include('includes/GeneralFunctions.php');
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
setInterval(function(){
$('#test').load('#test');
},5000);
</script>
<div id="test">
<?php
$l1 = $con->prepare("SELECT msg, topic FROM messages ORDER BY id DESC");
$l1->execute();
$cntmsg = $l1->rowCount();
foreach($l1 as $r1)
{
echo $r1['topic']."<br />";
echo $r1['msg']."\n";
}
?>
</div>
Use ajax in your SetInterval function.
ajax() method is used to perform an AJAX (asynchronous HTTP) request
$.ajax({url: "demo_test.txt", success: function(result){
$("#div1").html(result);
}});

How do I remove javascript tag 'type="text/javascrip"' on html code in PHP

I wish to remove 'type="text/javascrip" inside this code :
<img id="banlink" class="w-100" src="block_images_9.jpg" alt="Homepage Banner"/>
<script type="text/javascript" src="epto.min.js"></script>
<script>
$(window).ready( function() {
var m = document.createElement("div");
....
How to make it in PHP ?
Thanks for your help.
Your question is very unclear, I do not fully understand what you want to do. Please elaborate.
You can have javascript inside of php as follows:
<?php
echo '<script>
$(window).ready( function() {
var m = document.createElement("div");
....
</script>';
?>
OR
<?php
echo '<script type="text/javascript" src="epto.min.js"></script>';
?>
You can also reference your javascript files as
<script src="epto.min.js"></script>
without the type="text/javascript" as stated in your question.

php echo not working for strtotime function while assigning it to a javascript variable

This works fine
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
var timeLeft = <?php echo '1279'; ?>;
$("#time").text(timeLeft);
});
</script>
</head>
<body>
<div id="time"></div>
</body>
</html>
but this doesn't
<script>
$(document).ready(function(){
var timeLeft = <?php echo strtotime("now"); ?>;
$("#time").text(timeLeft);
});
</script>
It gives error Uncaught SyntaxError: Unexpected token < .
It is a php extension file.
Try this:
<script>
$(document).ready(function(){
var timeLeft = '<?php echo strtotime("now"); ?>';
$("#time").text(timeLeft);
});
</script>
Directly using php in javascript is very dangeorous. Because timetostr may output some warnings about timezone etc and your code will be broken without any visual clue.
In my projects, If I have to use any php output in my javascript, I create hidden divs and access the value from javascript:
<div class='hidden' id='values'>
<div class='val-time'><?php echo strtotime("now"); ?></div>
</div>
<script>
$(document).ready(function(){
var timeLeft = $("#values .val-time").html();
$("#time").text(timeLeft);
});
</script>
So whenever you want to debug your code, simply remove "hidden" class from #values and look at which values are ouputted by PHP.

Can't use PHP value from jQuery.load() content

I'm working on a project that uses jQuery modals that dynamically load their content on a button click.
I am having an issue using this loaded content like I would normally.
Here is an example of my problem
<script type="text/javascript>
click function{
load modal{
open: $('#modalID').load('phpfile.php?id=<?php echo $id ?>');
}
</script>
That all works fine, but when trying to use jQuery within the "phpfile.php" is where the problem lies
<?php
$id = $_GET['id'];
$db = USE ID TO GET DATABASE INFO; //works fine here
?>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
When I click the button, I get the alert but it just says test and I don't get the ID like I should.
Thanks in advance for your help and suggestions on this one!
You have pasted pseudo code so it is difficult to say what is wrong in your code.
The following should work, give it a try:
File 1.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?
$id = "10";
?>
<script>
$(document).ready(
function(){
$('#modalID').load('phpfile.php?id=<?php echo $id ?>');
});
</script>
<div id = 'modalID'></div>
And phpfile.php:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<?php
$id = $_GET['id'];
?>
<script>
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>
<input type="button" id = "buttonID" value="click me">
Please check on the do you have any errors in your browser's console. The above code should work. The possibilities are like if you don't get your $_GET['id'] or any script errors will prevent execution of your code.
I have created a sample php file which is working fine.
<?php
$id = rand();
?>
<button id="buttonID"> Click </button>
//ECHO SOME HTML HERE
<script type="text/javascript">
$('#buttonID').on('click', function(){
alert('test <?php echo $id ?>');
});
</script>

Debugging javascript code

My script part in my php page is like this
<script type="text/javascript" >
alert('Out');
if (<?php echo $delFlag; ?> == 1) {
alert('ting');
}
</script>
While the code below is what I get from the Chrome Developers tool (Resource>XHR) (which I'm using for debugging)
<!-- For javascripts -->
<script type="text/javascript" >
alert('Out');
if (1 == 1) {
alert('ting');
}
</script>
The alerts are not popping up.
What is wrong with my codes??
Update
I've my codes in a file add-line.php
<script type="text/javascript" >
// $(function() {
if (<?php echo $timer; ?> == 1) {
//alert('Line');
var timenow = <?php echo time(); ?>;
var dataPass = 'lineId=' + <?php echo $lineId; ?> + '&storyId=' + <?php echo $storyId; ?> + '&timenow=' + timenow;
$.ajax({
type: "POST",
url: "proc/updateDb.php",
data: dataPass,
cache: false,
success: function(){
// Show error if error is there
}
});
}
// });
</script>
Now in the updateDb.php I m processing some CRUD thing using the POST values and then determine $delFlag value. Then at the end of this file, I've my scripts as (same as the first one above)
<script type="text/javascript" >
alert('Out');
if (<?php echo $delFlag; ?> == 1) {
alert('ting');
}
</script>
But this script is not being executed at all it seems.
As I notice you are using ajax, you that script would run if you add it to the DOM.
for example, if you use $.ajax()
$.ajax({
url: 'some/url/',
type: 'html',
method: 'get',
success: function(data){
$('head').append(data);
// data here would be the response from the server...
}
});
You tried this:
<!-- For javascripts -->
<script type="text/javascript" >
var number = parseInt("<?php echo $delFlag; ?>");
alert('Out');
if (number == 1) {
alert('ting');
}
</script>
You can do it without the quotes if you want.. but if you using some editor they whill markit as a error.
Try like that. Error message will prompt and try to fix it.
<script type="text/javascript">
alert('Out');
try
{
if (<?php echo $delFlag; ?> == 1) {
alert('ting');
}
}
catch(err)
{
alert(err.description);
}
</script>

Categories