I have an autosuggest done in PHP which on selection will provide to me the id of the item selected from the auto suggest. I would like to post the id that was selected to the server. What would be the best practice to store this variable and sent the same to the server via HTTP Post in PHP?
Steps:
List data from a Auto Suggest Text Box. (This is done using javascript).
Select an Item from the Auto Suggest and also store the key value for the selection in a data store that can be sent over HTTP Post. (The data store is what I am not able to understand)
Post the form to the server.
I tried to store the value into a hidden field and my javascript breaks, not sure why. Is there any other data store mechanism that I can use other than hidden fields?
Could someone please guide me through this as I am pretty new to PHP.
Thanks and Regards
Abishek R Srikaanth
Your javascript may be breaking due to unescaped ' or some other parsing error. Check your browser's javascript console (firefox and chrome have it) for the error.
Hidden field is the way to go. So instead of using other method, try to fix the JS code.
First of it would help use tremendously if you showed us some code(like other users asked for) to work with. This enables me/us to give you a better answer. You don't have to show all the code but just enough to give us a demonstration(if code should remain private). You can use http://jsfiddle.net/ to host the code easily for us.
Are you using any framework? I would advise you to use JQuery because it is a really nice framework and I believe you could achieve your task with JQuery easily.
I think you should be using HTTP GET instead of POST because POST should not be cached(idempotent) according to HTTP-Specs and I believe your data can and should be cached?
Related
I have a hidden input, as follows -
<input type='hidden' id='question_number' name='question_number' value='100'/>
I need the value of question_number into a php variable, without submitting.
I know that in jquery it can be done by -
var q_no = $('#question_number').val();
but I don't know how to send it to php variable.
Also I don't want to create a new file just to get the 'question_number' by ajax.
I don't know if this is possible without submitting and without ajax.
p.s. I found a related question, but I couldn't not found working answer there.
Edit-1. I know that PHP is server-side and jquery/javascript is client side, before posting this question. But, I was thinking, if a php code, can search through entire page of this particular input type, and then parse its value. I am NOT a php expert, so I can't construct this on my own. I need this value in PHP for further coding.
Edit-2. Please read Edit 1, before negative voting.
That is not possible. Javascript runs on client, and PHP runs on the server.
As derdida said, you are missing the point as client side coding (ex: jQuery) is completely different from server side coding (ex: PHP). You should search for "client server side code" on Google and start from there ;)
For a system I am developing I need to programmatically go to a specific page. Fill out one field in the form (I know the id and name of the input element), submit it and store the results.
I have seen a few different Perl, python and java classes that do this. However I would like to do this using PHP and havent found anything as of yet.
I do have the permission to do this from the site i am getting the information from as well.
Any help is appreciated
Take a look at David Walsh's simple explanation.
http://davidwalsh.name/curl-post
You can easily store the response (in this example, $result) in your database or logfile.
Usually PHP crawlers/scrapers use CURL - http://php.net/manual/en/book.curl.php.
It allows you to make a query from the server where PHP runs and get response from the website that you need to crawl. It returns response data in plain format and parsing it is up to you. You can manually check what does the form submit when you do it manually, and do the same thing via curl.
You also may try phpcrawl (http://phpcrawl.cuab.de), seems to fit all your needs.
(See "addPostData()"-method)
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.
For example I have a Javascript-powered form creation tool. You use links to add html blocks of elements (like input fields) and TinyMCE to edit the text. These are saved via an autosave function that does an AJAX call in the background on specific events.
The save function being called does the database protection, but I'm wondering if a user can manipulate the DOM to add anything he wants(like custom HTML, or an unwanted script).
How safe is this, if at all?
First thing that comes to mind is that I should probably search for, and remove any inline javascript from the received html code.
Using PHP, JQuery, Ajax.
Not safe at all. You can never trust the client. It's easy even for a novice to modify DOM on the client side (just install Firebug for Firefox, for example).
While it's fine to accept HTML from the client, make sure you validate and sanitize it properly with PHP on the server side.
Are you saving the full inline-html in your database?
If so, try to remake everything and only save the nessesary data to your backend. ALL fields should also be controlled if they are recieved in the expected way.
All inline-js is easily removed.
You can never trust the user!
Absolutely unsafe, unless you take the steps to make it safe of course. StackOverflow allows certain tags, filtered so that users can't do malicous things. You'll definately need to do something similar.
I'd opt to sanitize input server side so that everyone gets their input sanitized, whether they've blocked scripts or not. Using something like this: http://www.phpclasses.org/package/3746-PHP-Remove-unsafe-tags-and-attributes-from-HTML-code.html or http://grom.zeminvaders.net/html-sanitizer implemented with AJAX would be a pretty good solution
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.