So I have an interesting problem
I am having issues with the jquery .load() function. I have a file structure as follows
index.php
|
|---header.html
|---body.html
|---footer.html
index.php sets three variables as so $variable_one = new Database('passedQuery');
header.html, body.html and footer.html are then included into index.php
In body.html I have something similar to
<div class="span4 well">
<h3>Current Statistics</h3>
<hr>
<table class="table table-striped">
<thead>
<tr>
<th>Users</th>
<th>Event One</th>
<th>Event Two</th>
<th>Event Three</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $var1->result[0][0]; ?></td>
<td><?php echo $var2->result[0][0]; ?></td>
<td><?php echo $var3->result[0][0]; ?></td>
</tr>
</tbody>
</table>
</div>
I also have a form in body.html which is submitted through a generic jquery function as so
$(document).ready(function(){
$('.btn').bind('click', function(e){
e.preventDefault();
var form = $(this).parents('form');
var vals = $(form).serialize();
var form_id = $(form).attr('id');
$.ajax({
url: $(form).attr('action'),
type: $(form).attr('method'),
data: vals,
dataType: 'json',
success: function(data){
console.log("Success");
$('#information').html('<div class="alert alert-success">Great Success! :)</div>');
setTimeout(function() {
$('#content').load('views/partials/body.html');
}, 5000);
},
error: function(a, b, c){
console.log("Error");
$('#information').html('<div class="alert alert-error">Epic Failure</div>');
setTimeout(function() {
$('#content').load('views/partials/body.html');
}, 5000);
}
});
});
});
When I submit the form and reload the body the PHP variables that were echo'd in the html are now gone and I get an apache error similar to the following
[dateTime] [error] [client IP] PHP Notice: Undefined variable: variable in /path/to/body.html on line 133, referer: myReferrer
I asked a few of my friends about this and they were all stumped, as was #jquery of Freenode. Anyone have any ideas?
UPDATE - FORM CODE AND index.php CODE
Form
<div class="span4 well">
<form class="form-horizontal" id="admin_add" name="admin_add" method="post" action="routes/to/add_admin.php">
<fieldset>
<legend>Add Administrator</legend>
<div class="control-group">
<label class="control-label" for="select03">Select a user</label>
<div class="controls">
<select id="select03" name="user">
<?php
foreach($var1->result as $key => $value)
echo "<option>".$value[0]."</option>";
?>
</select>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary form-submit-button">Submit</button>
</div>
</fieldset>
</form>
</div>
Index
$var2 = new Database('query', 'SELECT COUNT(uid) FROM attendees WHERE eid = 1');
$var3 = new Database('query', 'SELECT COUNT(uid) FROM attendees WHERE eid = 2');
/**
* List users to add to database
*/
$var1 = new Database('query', 'SELECT name FROM users WHERE admin=0');
/**
* Include all the visuals
*/
include('views/partials/header.html');
require_once('views/partials/body.html');
include('views/partials/footer.html');
When you load body.html the first time I guess that you're loading it using php's include() function. Well, what this does is "concatenate" the php so on the server side it's like its one long php file. This works with your variables because its as if there is no break in the code.
When you use jQuery's load() functions this happens client side. What is happening is that you're getting the html output of JUST body.html and you are dynamically (on the client side) putting that content into the client's page.
The problem you're having then is that the PHP is no longer concatenated. load() takes body.html in its singularity and puts it into the page.
Another way to look at it is that when someone goes to index.php the php is parsed, all the variables that are echo'd are turned into bog standard text/html and shipped to the browser. Once there they are no longer php variables, they are just text/html. Making a new request to body.html via javascript means that those variables you're referencing are no longer available. They've already been converted to HTML and shipped to the browser, then the php stopped, and this is a new request.
I don't know how you want to fix this to be honest. You might want to look into a templating system like smarty. Or you might want to put your functions into a file called functions.php include it into your body.html and then reference those functions in your body.html rather than relying on code in the index page.
i.e. your body.html will look like this:
<?php include "functions.php"; ?>
<div ...>
<?php
$var1 = getVar1(); // function in functions.php
echo $var1->result[0][0];
// ... etc
?>
</div>
Then when it is called from javascript, before it is delivered to the browser, the php will be evaluated and everything will work.
Hope I've explained this well dude. :)
When your jQuery request gets to the server it will be serving you back body.html, which doesn't have any PHP processing, so your variables set within index.php, don't exist.
Related
We are on progress creating web page for live search and update file in bulk using JQUERY AJAX method. The files consist of index.php (for display to user and Javascript), and multiple_update.php (for fetch and update file in bulk). Initial reference we got is from here from webslesson website, but it does not have any reference for searching the record, hence we search for help for our journey.
Below is our index.php file
<div class="content-wrapper">
<div class="content-heading">
<div>STO Monitoring<small>Tables, one step forward.</small></div>
<div class="ml-auto"><input type="text" id="search" placeholder="Search" /></div>
<div id="display"></div>
<div class="ml-auto">
<button class="btn btn-primary btn-lg" type="button" data-toggle="modal" data-target="#myModalSmall"><i class="fas fa-plus-square"></i> Add Record</button>
</div>
</div>
<form method="post" id="update_form">
<div class="card">
<div class="card-body">
<table class="display" id="example" style="width:100%">
<thead>
<tr>
<th width="3%"></th>
<th width="5%">No</th>
<th width="15%">STO</th>
<th width="20%">PN</th>
<th width="8%">Qty</th>
<th width="10%">From</th>
<th width="10%">Dest</th>
<th width="15%">Status</th>
<th width="14%">Remark</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div align="left">
<input type="submit" name="multiple_update" id="multiple_update" class="btn btn-info" value="Multiple Update" />
</div>
</div>
</div>
</form>
</div>
.....
Below is script inside our index.php, the one we suspect need troubleshoot at the moment.
<script>
function fill(Value) {
$('#search').val(Value);
if (name == "") {
$("#display").html("");
}
}
$(document).ready(function(){
function fetch_data()
{
$("#search").keyup(function() {
var name = $('#search').val();
if (name == "") {
//Assigning empty value to "display" div in "search.php" file.
$("#display").html("empty");
} else {
$.ajax({
url:"multiple_select.php",
method:"POST",
dataType:"json",
data: {
//Assigning value of "name" into "search" variable.
search: name
},
success:function(data)
{
var html = '';
for(var count = 0; count < data.length; count++) {
html += '<tr>';
html += '<td><input type="checkbox" id="'+data[count].num+'" data-num="'+data[count].num+'" data-sto="'+data[count].sto+'" data-pn="'+data[count].pn+'" data-qty="'+data[count].qty+'" data-supplant="'+data[count].supplant+'" data-dest="'+data[count].dest+'" data-stat="'+data[count].stat+'" data-remark="'+data[count].remark+'" class="check_box" /></td>';
html += '<td>'+(count+1)+'</td>';
html += '<td>'+data[count].sto+'</td>';
html += '<td>'+data[count].pn+'</td>';
html += '<td>'+data[count].qty+'</td>';
html += '<td><span class="btn btn-oval btn-primary">'+data[count].supplant+'</span></td>';
html += '<td><span class="btn btn-oval btn-warning">'+data[count].dest+'</span></td>';
html += '<td>'+data[count].stat+'</td>';
html += '<td>'+data[count].remark+'</td></tr>';
}
$('tbody').html(html);
}
});
}
});
}
fetch_data();
$(document).on('click', '.check_box', function(){
.....
</script>
We modify the AJAX to see if the input can be catched by the network, below is code for multiple_update.php
<?php
include('multiple_connection.php');
$name = $_POST['search'];
echo $name;
$query = "SELECT * FROM matreq_list, sto_list WHERE matreq_list.sto = sto_list.sto AND sto_list.sto LIKE '%$name%' LIMIT 5;
$statement = $connect->prepare($query);
if($statement->execute()) {
while($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$data[] = $row;
}
echo json_encode($data);
}
?>
We want to make every search is captured via AJAX, and respond from network will be live-reflected in our index file. Below is our expected final result (this result is without "LIKE" in mysql statement to show the result only) :
And we confirm AJAX can handle our input, below is the image :
--UPDATE-- Below is error messages :
Error messages
However, after we fired the input, nothing cames up in our index.php file. Network shows good respond, but the HTML is not responding the way we expected it to do. Please kindly advise us sir, what is wrong with our method and what should we fix?
Thank you and appreciate your kind help in our case
=====UPDATE=====
2020-07-02 : As mentioned by mr Nadir Baoun, tried to change the order of jquery.js and put it above the bootstrap.js, and somehow my table now able to search some part or whole part of the data.
Before :
.....
<script src="vendor/datatables.net/js/jquery.dataTables.js"></script>
<script src="vendor/datatables.net-bs4/js/dataTables.bootstrap4.js"></script>
<script src="vendor/datatables.net-responsive-bs/js/responsive.bootstrap.js"></script>
<script src="vendor/jquery/dist/jquery3.5.1.js"></script>
<script src="vendor/datatables.net/dist/js/jquery.dataTables.min.js"></script>
After ordered : I move the jquery to the top of all codes.
and below is network screenshot :
After change order of javascript
Surprisingly, this work well :D
The HTML isnt responding the way you want is because you have JavaScript errors thus your response code wont function accordingly.
First thing include your jquery file before bootstrap.
This should solve the " cannot read property fn of undefined " error.
Please update your post with debug messages in the success param of your ajax request after doing what i have mentionned above
After thoroughly reading on various articles with a guide from Mr Nadir Baoun, my problem is now fixed by changing the order of the script, putting the jquery script before the bootstrap script.
Similar answers also posted in stackoverflow website :
script order for jquery with bootstrap
Thank you :)
After much Googling, I'm on the cusp of success (I think).
I have a fairly large page, overview.php that contains the following snippets:
<script>
function getSummary(id)
{
$.ajax({
type: "GET",
url: '_table_category.php',
data: "catid=" + id,
success: function(data) {
$('#summary').html(data);
alert('Successfully called');
},
error: function(jqxhr, status, exception) {
alert('Exception:', exception);
}
});
}
</script>
Down the page, I iterate over an array and populate a table. The first column is as follow:
<td> <a onclick="getSummary('<?php echo $expenses[$k]['categoryid']; ?>')"> <?php echo $expenses[$k]['shortdesc']; ?> </a> </td>
And then further down on the page, there is a div that will contain the details:
<div class="box">
<div class="box-header"> <h3 class="box-title"> Details </h3> </div>
<div class="box-body">
<div id="#summary"> Select a category to see the details. </div>
</div>
</div>
No errors upon loading the page, nor by clicking on the 'link'. If I click on the link, it pops up with the 'Successfully called' alert as expected.
Looking in the Network tab of the developer tools in Chrome, it calls the _table_category.php page (with a value for catid), and if I click on the link it lists (for example _table_category.php?catid=35), the HTML in the preview pane is correctly formatted HTML.
If I then paste the HTML inside the div manually, it looks exactly as hoped.
This makes me think I am missing something fairly obvious and I am just not actually replacing the div with the result?
Your div has a bad ID.
<div id="#summary">
It should just be:
<div id="summary">
The # is just there in the selector (it's used to specify "select by ID").
In my application I'm displaying the content of a file named test.php using ajax to a table with id fileContents. I'm using highlight_file() to highlight the code and it is working fine. I have a Save button in the page to save the div content back to the file.
<script language="JavaScript">
$( document ).ready(function() {
$.ajax({
url: 'test_file.php',
type: 'POST',
data: {
filePath: 'test.php'
},
success: function(data) {
$('#fileContents').html(data);
}
});
});
function saveFileContents() {
console.log($('#fileContentDiv').html());
}
</script>
<table id="fileContents">
</table>
<input type="button" value="Save" onclick="saveFileContents()">
test_file.php
<tr>
<td>
<div id="fileContentDiv" contenteditable="true">
<?php
echo highlight_file( $_POST['filePath'] );
?>
</td>
</tr>
test.php
<?php
echo "hai";
?>
I tried to edit the div and add a new line of code to the second last line $i=0; and when I click the Save button, I got console like this:
<code><span style="color: #000000">
<br><br><?php </span></code><div><code><span style="color: #000000">echo "hai";</span></code></div><div><code><span style="color: #000000">$i =0;</span></code></div><div><code><span style="color: #000000">?></span></code> </div>
I need to save the php code only back to the file(along with line breaks). I tried using $('#fileContent').text(). But it is saving the content in one line.
Can anyone help me to fix this? Thanks in advance.
The question is not a duplicate of this, although the mentioned question has some very interesting answers and helped me to understand my problem better but it doesn't fulfill my need. Let me explain my scenario.
I have an asp.net mvc website, having notification functionality and real time data updates by signalR & SQL dependency. User authentication is being done by using Identity 2.0.
Only authorized users are allowed to see the updated data/notifications. Furthermore the notification/updates vary user to user. I've achieved this by implementing custom UserId provider and using Identity's UserId.
Now I want to achieve following goals.
Easy part: Display the content of the page (dashboard) in other websites regardless of their development language. It can be injected inside existing page or wherever they (other websites) want.
Hard part: Show the real time notifications & updates on the integrated portion based on logged in user.
What would be the best course of actions to perform in this scenario?
Update
Since the original question was put on hold due to not specifying the details, so here are the details on which I would like to get suggestions.
I've a asp.net mvc website running at localhost:54603, Following is the action of the home controller
public ActionResult Index()
{
HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
return View();
}
Index view has some signalR functionality on it. Below is the startup configuration of signalR to allow CORS since I my goal is to expose the index page and its whole functionality to clients (running php etc)
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
map.RunSignalR(hubConfiguration);
});
I've created notifcationHub and following is my code at front end (using proxy).
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
$.connection.notificationHub.url = 'http://localhost:54603/signalr';
var notification = $.connection.notificationHub;
// Client side method for receiving the list of notifications on the connected event from the server
notification.client.refreshNotification = function (data) {
$("#notificationTab").empty();
$("#cntNotifications").text(data.length);
for (var i = 0; i < data.length; i++) {
$("#notificationTab").append("<tr> <td> " + data[i].Id + "</td> <td>" + data[i].Text + "</td> <td>" + data[i].CreatedDate + "</td></tr>");
}
}
//Client side method which will be invoked from the Global.asax.cs file.
notification.client.addLatestNotification = function (data) {
$("#cntNotifications").text($("#cntNotifications").text() + 1);
$("#notificationTab").append("<tr> <td> " + data.Id + "</td> <td>" + data.Text + "</td> <td>" + data.CreatedDate + "</td></tr>");
}
// Start the connection.
$.connection.hub.start().done(function () {
//When the send button is clicked get the text and user name and send it to server.
$("#btnSend").click(function () {
notification.server.sendNotification($("#text").val(), $("#userName").val());
});
console.log($.connection.hub.id);
}).fail(function (data) { console.log('Could not connect' + data); });
});
</script>
and Html is following
#{
ViewBag.Title = "Home Page";
Layout = null;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script>
<script src="#Url.Content("~/signalr/hubs")"></script>
#*<script src="~/signalr/hubs"></script>*#
<div style="width: 70%; padding: 20px">
<div class="panel panel-primary">
<div class="panel-heading">
<! – To show notification count-->
<div style="float: left" class="panel-title">Notifications</div>
<div style="float: right" class="badge" id="cntNotifications"></div>
<div style="clear: both"></div>
</div>
<div class="panel-body">
<! – To show All the notifications-->
<table class="table table-striped table-hover ">
<thead>
<tr>
<th>#</th>
<th>Text</th>
<th>Created Date</th>
</tr>
</thead>
<tbody id="notificationTab"></tbody>
</table>
</div>
</div>
<! – Add panel notification to send notification, Make sure that user enters the user id of the domain they are logged into -->
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Create Notifications</h3>
</div>
<div class="panel-body">
<div class="form-group">
<label class="control-label" for="focusedInput">Notification Text</label>
<input class="form-control" id="text" type="text" value="">
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Send To</label>
<input class="form-control" id="userName" type="text" value="">
</div>
<a id="btnSend" style="cursor: pointer" class="btn btn-primary">Send Notification</a>
</div>
</div>
</div>
The page runs fine as expected and connection id is being logged in console.
Now I've created another html page and following is its code
<html>
<head>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.2.0.min.js"></script>
</head>
<body>
<aside>
<div class="col-lg-2">
<ul>
<li>this</li>
<li>is</li>
<li>aside</li>
</ul>
</div>
</aside>
<article>
<div class="col-lg-10">
<div id="something">
</div>
</div>
</article>
<script src="http://localhost:54603/signalr/hubs"></script>
<script>
$(document).ready(function () {
$.ajax({
url: "http://localhost:54603/Home/Index",
dataType: 'html',
crossDomain: true,
cache:true
}).done(function (data) {
$('#something').html(data);
});
});
</script>
</body>
</html>
Ajax request is returning the html but the signalR throws error. following are the console messages.
GET http://localhost:54603/Home/Index 200 OK 1.18s
Could not connect Error: Error during negotiation request.
What am I doing wrong which is causing this error?
It's difficult to pin down exactly what you need to do, cause the question is rather broad, but I hope that these points help(ed) somewhat:
SignalR cross-domain uses JavaScript on the client side and CORS on the server side, so you're covered on that front. You could easily create a JavaScript file that can connect into the SignalR channel without any changes to PHP code other than to include the JavaScript in the client's page.
You could quite easily create a single script on your server that the client can reference on their page that pulls everything required on to the client's page. Because the script executes from your own server I don't think you'll need to worry about CORS cause it's not truly 'cross site scripting', because the code executing the requests will be 'owned' by your server/domain.
As for authentication, if it's only your script that needs access to the authentication, I think that means that you can make use of your domain's authentication too (cookies etc.) I'd have to check up on that one though. Ultimately you'd authenticate them using a HTTPs request to your domain from the included script?
Here are some useful threads that might help with that one:
Cross-Domain Cookies
Creating a javascript cookie on a domain and reading it across sub domains
I'm building a website where the admin can make settings for the website. I would like the admin settings page to have a similar "feel" as the rest of the website, which has some nice looking jQuery features.
On the admin site there's a hidden div, which is shown when one of six links has been clicked. I'd like the content of the hidden div to change content before showing itself. I'm not sure how to do this. I could have a div box for every link on the page. But this becomes pretty cumbersome since I'd need to repeat my css and jquery for every link. I imagine that this, somehow, can be done with some javascript/jquery code that determines which link that was click and then decides which php function to call inside the hidden div. Then the php could "echo" out the content of the div, which then could be shown.
How could one do this?
My HTML/jQuery code is as follows:
--- The html links ---
<table id="settings">
<tr>
<td>
<img src="images/folder.gif" alt="" height="100"/>
</td>
<td>
<img src="images/folder.gif" alt="" height="100"/>
</td>
<td>
<img src="images/folder.gif" alt="" height="100"/>
</td>
----- The Hidden div -----
<div id="dashboard_box">
<div class="dashboard_inside">
<form action="#" method="post">
<p style="font-size:20px; font-weight: bold;">Change color</p>
</br>
<fieldset>
<? load_content1();?>
</fieldset>
</form>
</div>
</div>
---- jquery code (working)----
var mouse_is_inside = false;
$(document).ready(function() {
$(".action").click(function() {
var loginBox = $("#dashboard_box");
if (loginBox.is(":visible"))
loginBox.fadeOut("fast");
else
loginBox.fadeIn("fast");
return false;
});
$("#dashboard_box").hover(function(){
mouse_is_inside=true;
}, function(){
mouse_is_inside=false;
});
$("body").click(function(){
if(! mouse_is_inside) $("#dashboard_box").fadeOut("fast");
});
});
You probably want to use ajax to load the content from the server. Take a look at jquery's .load() method: http://api.jquery.com/load/
You could include a data attribute per link:
<a class="action" data-content="content.php?method=load_content1"></a>
<a class="action" data-content="content.php?method=load_content2"></a>
js would look something like this:
$(".action").on('click', function() {
$("#dashboard_box fieldset").load($(this).data('content'), function() {
var loginBox = $("#dashboard_box");
if (loginBox.is(":visible"))
loginBox.fadeOut("fast");
else
loginBox.fadeIn("fast");
}
return false;
});
Then, in your content.php file you could check the method parameter in the url to determine what content to return.
My php is a little rusty, but something like this:
<?
call_user_func($_GET['method']); // i'm not sure how safe this is. you may want to be more explicit
?>
You can just add data attribute in each of your link's
<a href="#" data-url="content1.php" ..
Then on click of any of the a you can get the php to be called.
$('a').on('click',function(){
var phpFunctionToCall = $(this).data('url');
});
You probably need to make ajax call to load content into your fieldset As this <? load_content1();?> run's on server and javascript have no control over it.
Thanks for all the help. this is what I ended up doing.
--- HTML ---
<a class="action" data-content="content.php?method=load_content2"></a>
---Jquery---
var mouse_is_inside = false;
$(document).ready(function() {
$(".action").click(function() {
var phpFunctionToCall = $(this).data('content');
$('#indhold').load(phpFunctionToCall);
var loginBox = $("#dashboard_box");
if (loginBox.is(":visible"))
loginBox.fadeOut("fast");
else
loginBox.fadeIn("fast");
return false;
});
$("#dashboard_box").hover(function(){
mouse_is_inside=true;
}, function(){
mouse_is_inside=false;
});
$("body").click(function(){
if(! mouse_is_inside) $("#dashboard_box").fadeOut("fast");
});
});
--- PHP (shortened)---
function load_settings_panel($settingOnRequest) {
return $settingOnRequest;
}
$result = call_user_func('load_settings_panel', $_GET['method']);
echo($result);