I'm having a problem with my autocomplete. It works on another one of my pages, but on this one, it doesn't work. It's returning the correct number of entries, but they are all "blank" (or at least black so I can't see it), and selecting one does not put it into the text field either.
I'm using this: http://papermashup.com/jquery-php-ajax-autosuggest/
My page right now looks like
Any suggestions?
Thanks!
I'd post my code, but it's pretty much exactly what's on the site linked above, with some variables changed, and embedded into a PHP. Let me know if you want to see it (I don't want to paste it here and make the page huge and fugly).
Oh and this is taking it from a column in a MySQL database.
I think this link is quite useful to understand the technique. After you got the AJAX technique, you may simply change your php files which can run sql queries etc. You may show the results in that way with a simple div, very trivial css implementation would be enough. I think the hardest part is solved here:
http://www.w3schools.com/ajax/ajax_aspphp.asp
maybe your problem lies within encoding, jquery needs utf8 in the default settings, but without any code, I can only speculate...
try utf8_encode($output) instead of only returning the output...
also maybe your ajax request awaits a specific datastructure (json/xml/ etc)
Related
I have a ecommerce store website running with WordPress. I'd like to include a section with a -random custormer's product review, so that every time someone access the page, there will be a different comment there.
I'm not used to PHP, but I managed to create a shortcode which takes a random comment and returns the proper HTML. It is working fine (in eddition mode, every time I insert the shortcode a different comment appears).
My issue is that when I leave the page and return, the previous one is still there. I believe that it is being caused by cache, but I wouldn't like to disable the cache for the whole page.
How you I force the shortcode run again (I don't know if it is the right way to explain) and make sure that at every access, a different comment appears?
One solution I thought is to have JS code which would do preaty much the same thing my PHP code does, using Woocommerce API to get the data. But I'm wondering if there is a simpler solution to do that, like forcing the specific section not being cached or re-run the shortcode.
Thanks!
JS can't do what PHP does here: at most it can create an AJAX-call to the backend that then runs a query for a random comment and returns it. You need to render it thereafter. It's fairly standard, but overkill for your case.
Instead, you're going to want to check whether your caching mechanism supports ESI or something else that excluded parts of your code from being cached.
I thought I should ask this before moving forward with my scripting project and later on realizing that I might be doing something wrong. Might as well save my own time in the first place by checking if what I'm doing is indeed correct.
I'm pretty new to PHP and JQuery and still getting the hang of them both. I recently came across a problem in which I wanted to run some PHP code after executing a certain function using JQuery, so after doing some reading online I got the idea to create a PHP file which does just what I want to do and POST to it from my JQuery function with the needed variables. That way I achieve my server-side and client-side goals on the same time. However!
It doesn't look right to me. At all. I find myself having at least ten include files for one simple page. Those files get included in my HTML code when the form loads, and after refreshing a certain DIV with my JQuery function, they get reloaded. That way I can keep my script dynamic, I doubt there's something else I could do in order to keep it that way.
However I often need to update stuff after executing a certain JQuery function, hence I call a similar file to the loading one, but this time it's a file which I transfer some parameters to (using the POST method, through JQuery), and execute the desired action in the file itself.
Now, after briefly explaining my current situation and method of work, I'd like to know if what I'm doing is correct. In case you're still wondering what the hell I am talking about, I'd like to explain in detail what I mean:
Let's say I have my index.php file which prints data from my database and some additional lines as well. Once I click one of the page buttons, I want to update my database according to the form I've got implanted in my page as well, and then reload the DIV which contains that data ONLY. I do not want to reload the entire page.
Now if I wanted to do that without JQuery that would have been easy. I would POST the data to the same form and update it if the POST parameters are indeed valid. However, there's no way to refresh that DIV without JQuery, so I came up with something similar to this:
$.post("/files/dosomething.php", { taskID: _taskID }, function(){
$("#div_tasks").fadeOut(800, function(){
$('#div_tasks').load('/files//load_div_A.php', function(){
$("#div_tasks").fadeIn(1500);
});
});
});
That's what I've been doing in my last week of learning PHP and JQuery. Now before I proceed any further than this, I'd like to know if it is indeed the right way to achieve my goal.
// EXAMPLE B:
I've got a DIV in my HTML code which prints a table of tasks, for instance. How do I print it? I include a file which echo's the table at the exact point where I want the table to be displayed. Then, in each row, there's a button called 'delete' which removes the selected row from the database. What I do is, using JQuery (due to the fact I want ONLY that DIV to be refreshed), I call another external file calling remove.php and send the index of the task I'd like to remove using POST. The file performs the server-side code and once it's done, I load the updated table into that DIV by calling the file I used to call in the first place from my main page. This time the table will be updated due to the fact it will read the updated data from the database.
Is that how I get this done? Is this the right way? It just doesn't seem right to me. I've never been codding that way and it seems a little bit messy.
Thanks in advance and sorry for the long ass question.
The problems you describe are exactly what the separation of concerns patterns (like MVC) where designed to tackle.
In your case from display instance to display instance the only thing that changes is how the response must be formatted and provided back to the user.
I would highly recommend looking at some of the more popular and well documented MVC Frameworks for your project (or if for nothing else just to play with and draw inspiration from, most of them and some pretty easy to follow starter tutorials you can run through in an hour or so).
It seems to me you are viewing the problem in a mindset compatible with these approaches and it probably feels clunky to you mainly because you are missing a lot of the structure and tooling that these frameworks provide.
If I am understanding the question correctly. I would think that it would be easier to post(using jQuery) to another script that performs the action you want to perform and only returns the result(html/json data) that you want. Right now you are making two requests to the server for something that seems to be tied together.
My suggestion would be to call one PHP script that performs the specific action that you want to perform(say an update to a news item for an example). Then return that data only in your response and then format that data how you want to in your div using jQuery.
$.post("/files/writetodatabase.php", { taskID: _taskID }, function(data){
$('#div_tasks').html(data) // this is assuming you return html, other wise you // could return JSON data and use it here
});
I tried to make that as clear as possible, difficult to explain when typing.
I'm working on a editing tool (type of a simple CMS) in PHP/MySQL for a product catalog. I have search the Internet for a solution but I don't even know what to search for. So now my hope is on you guys.
I have a form where you can put all kinds of data like part.no, description an so on. All of this data is saved into a MySql table (items). I also have a table with predefined specifications.
What I want to do, and that I can't find a solution for, is to have a dropdown meny (or similar) and a add button to add a row for each related specifications without saving the whole form each time. I want to save first when all specifications is selected.
So, can I use PHP for this or do I need jQuery/Javascript or similar? I know it's possible, have seen it in OpenCart :-)
I hope someone understands my question. It's hard to explane i a language I'm not fully manage.
Regards
Client-side vs Server-side
Javascript: This sits in the user's browser. So anything you want to move in the user's browser will be done with JavaScript. This is "client-side"
PHP: This site on the server, so takes inpute from the user's browser and gives back a response (generally HTML, but can also be JSON or XML which is read by Javascript.). This is "server-side".
Libraries
jQuery: This is a set of functions written for Javascript to make it easier. So it runs in the user's browser and makes it easier for you to write bits that move on the screen.
You get similar libraries that help you write PHP (commonly called "frameworks") and there are many others for javascript as well.
Where to start
Write your HTML page as you want it to look. Keep it simple for the first time.
Then write some javascript (possibly using jQuery) to move the menu. Google "jquery menu dropdown" or similar and you'll find a solution you cna customise.
Then write some PHP that gives you the HTML you wrote in '1'.
Then decide what's going to happen when you click on a link in the HTML, and repeat the process (write HTML, incorporate Javascript to make it move, write PHP to give HTML)
Then work out which bits of the HTML are common or structured and should come from a database.
Without writing it for you (in which case you'll never learn) best to start one bit at a time and build as your knowledge grows. Bucket loads of examples on the web when youreach a particular problem you need to solve.
After comment "[how to] make it possible to select and add single/multiple specifications (from another table) without saving the whole form each time a specs is added":
Growing with AJAX
What you are asking is AJAX - this is where you get Javascript to talk to the server, and for javascript to move bits on the page based on the results. jQuery is probably the easiest (and probably has best documentation / examples for the ajax, as well as moving the DOM).
Basically: you have an "event" that you trap in JavaScript, example
/// Using jQuery to trap a button click
$().ready( function() {
$("#ButtonID").click( function(e) {
e.preventDefault();
alert('Button Clicked');
});
});
Then you build in an AJAX call inside that event (also check out get or post as the syntax is easier, you just get less control). The AJAX wil send a request to your PHP server, and you can get PHP to return HTML which you can replace/insert using the DOM manipulation functions linked below (e.g. before, html etc) or, when you get more advanced, you'll send back JSON which is a data structure you cna more easily manipulate in JavaScript to stipulate what actions are required.
As above, without actually writing it for you, the best place to start is to read the docs and have a go. Google "jquery AJAX PHP table example" or similar and you'll find an example somewhere.
I'm thinking about going with Google charts for a project I'm working on. I have all my data on my own server and so I was wondering what is the best way to go about inserting this data into a chart, there are a few alternatives:
Create the DataTable object from data that is provided inline. That is, print all the data into the HTML document. This will crowd out everything else since I have a lot of data, but I don't know if this is important. This way we can avoid one HTTP request.
Dynamically create a .js files for every request, holding the data, and letting it be included with a script tag in the document.
Retrieve the data using ajax (Google suggests this in their documentation)
Using the chartwrapper and adding a datasource pointing to my own server. This would be equivalent to the above, I suppose, and functionally equivalent to (2).
So what is the most common solution? What do you usually solve this?
I wouldnt worry about crowding out your data. Printing it out into a javascript datatable wont be visible to the user, and the browser wont care. However I would suggest you only print out what you need for each page so you dont have more than required.
I think probably any of your solutions are fine, so pick the one that suits you best.
Hi I have a web form that sends a string to one php file which redirects them to a corresponding URL. I've searched about web form hacking and I've only received information about PHP and SQL... my site only uses a single PHP file, very basic etc. Would it be open to any exploits? I'm obviously not going to post the URL, but here is some code I was working on for the php file:
Newbie PHP coding problem: header function (maybe, I need someone to check my code)
Thanks
From that little snippet, I don't see anything dangerous. "Hackers" can enter pretty much anything they want into $_REQUEST['sport'] and thereby $searchsport, but the only place you use it is to access your array. If it's not found in your array.... nothing much will happen. I think you're safe in this limited scenario ;) Just be careful not to use $searchsport for...... just about anything else. Echoing it, or inserting it into a DB is dangerous.
Uh, it really depends. If you are inserting data into a MySQL DB without sanitizing, the answer is a huge yes. This is something you need to decide for yourself if you aren't going to show code.
The solution you've got in the linked question is pretty safe.
Every possible action is hardcoded in your script.
Nothing to worry about.
Though asking for the "web form like this" you'd better to provide a web form. Not the link to the question that contains a code that can be presumed as this form's handler.