Is it possible passing html elements attributes with a PHP form? - javascript

I would like to know if I can send html elements attributes (like id, class or title) with a classic post form and save them in a db.
I know this is possible with AJAX, saving the values in variables with javascript and then sending them to a PHP file with $.post or $.ajax
But is it possible with a normal post submit form?
Thanks to all

This is not possible. Only the value- and the name-attribute are sent to the server when you use a classic submit. There is no way to get more attributes then these two.
So, you are forced to use javascript.
The following might be an alternative to your proposed $.post-function: you can adjust a form and then submit it using javascript. So, apart from the two JQuery-functions you mention, there is also a form.submit() method. You will have to manipulate the value- and name-attribute of the form-elements and then use .submit() to send those to the server.

This is not possible, only names and values are posted.

Related

Using JavaScript function result in PHP

Is it possible to use the results of a JavaScript function in PHP?
Let me break down why. I have a table that has several columns, including a column that gives each row a checkbox. When a user presses an edit button, an onclick event is activated. The onclick event does a few things, but primarily it goes through the table and figures out which checkboxes are clicked, and returns an array of numbers which indicate the IDs of the entries of the selected rows.
What I want to know is how can I take these Ids and use them in my inline php code that should use the Ids to get corresponding data from a database and generate a new table.
Also, is there another way of doing this type of thing? This seems a little convoluted. I've tried XMLHTTPRequests to get the data directly with Javascript but I had trouble/couldn't get past using the GET method of an XMLHTTPRequest. My code would execute, but I wasn't sure how to get/use the data.
Any help is appreciated.
You can't use javascript values in your PHP statements because, imagine that the javascript is a client-side language, so it don't communicate with the server, and imagine that PHP is a server-side language, so it can't communicate with javascript. When you request a webpage, the request go to the server and the server will trait your request, and all output of this request (the webpage) is a code on client-side language and it is rendered with javascript, html and css. So, we can say that javascript have no idea of what is PHP, because, what PHP do is write a full html code and send to your browser render like a response. But you can use Ajax inside your client-side, there is a lot of tools developed in javascript that do the work of make requests to server-sides

How can I display and update PHP objects in HTML?

I know similar questions have been asked and answered before, but I see so many different answers that I'm wondering if somebody can give an answer for my specific issue.
I wish to display a list of Soccer players on a webpage, which are retrieved from a database. Each player needs a drop-down box, allowing him to be picked/unpicked/subbed in the squad. When this happens, the database needs to be updated, along with the webpage, to show his new position and to store the information. I know how to update the database with PHP, but the only way that I know how to do the webpage part, is with Javascript. I thought I'd cracked it when I fathomed out how to use 'onchange' to call a Javascript function, but that's brought up another problem. If I pass the ID of the player and his new position to the function, I'm unable to update the PHP object from inside the Javascript, because there's no way (that I know of) for the PHP code to read the passed variables. What I'd want to do inside the function, is take the passed ID parameter, use that to match up the player in the PHP array, then update him.
The only solutions that I can think of are either
A) Don't use PHP at all in the HTML page and instead pass the information to the page with json_encode... presumably then read it into a Javascript array/function. Then when the function updates the player, call an PHP file (can parameters be passed into the file this way?).
B) Somehow convert the parameters in the Javascript function to PHP. Is this possible?
C) Find another way to respond to the down-down box that calls PHP instead of Javascript. Again, not sure if this is possible.
D) Call a PHP function from within the Javascript function. Again, don't think it's possible?
Is there a simpler solution or do I go with one of the above?
Javascript should be used to enhance the user experience. It should not be mandatory for your page to work.
First do your page with HTML and PHP. HTML passes your form data via post to your PHP script. PHP then populates your database with that data.
Then add javascript if you want to enhance functionality.
With javascript you can do things like validate the form so that you don't need to send the page to PHP until is it properly validated thus by saving you a page load. Note: You should still validate using PHP afterwards, javascript is only for enhancement.
You can pass the data from javascript to PHP using ajax.

pass javascript variable to ajax

I am working on a site with many forms . Most of the forms will be submitted via jquery AJAX.
I have implemented recaptcha for security but the client does not like it as the words are sometimes difficult to read.
As an alternate I am looking at honeytrap method for less important forms. As I understand In this method I keep a field in the form hidden from normal users via CSS. So legitimate users will leave the field blank while a bot will fill it .
Now my question is since I am using jQuery AJAX for form submission, instead of hidden field if form , can't I just pass an extra variable (along with form field variables) from javascript to AJAX. (like var secretword = 1) AJAX will check this variable( if secretword == 1 ) and if it is empty(bot will not use javascript to sumbit form so var will be empty) then the form will not be submitted.For legitimate users javascript function will add "1" value to the variable.Is my method as secure as honeytrap?
NOTE: Website does not work without javascript so need not go into what happens if javascript is disabled.
Please provide me your valuable advice on this.
You're really over-thinking this.
If you only do legitimate submission via AJAX, make the form's action attribute wrong, and you've effectively accomplished what your proposed solution does: Anybody directly submitting the form without JS will have their submission ignored.
This obviously won't deter anybody from figuring out where you AJAX submissions are going and just spamming that endpoint directly, but neither will your proposed secretword=1 solution or a traditional hidden field.

Advanced Form Validation in JavaScript

I'm already familiar with how to use onSubmit to evaluate form content against RegEx to ensure it meets static parameters for acceptable content. What I'm wondering is if there is a way to further provide validation against a MySQL database, such as if you want to make sure an e-mail address hasn't been used yet before submitting a form and having to re-load the field data back into the proper places for correction.
Unless you sent all the valid email addresses to the client (you wouldn't) then you're going to have to do some sort of server roundtripping to validate the field, which is equivalent to submitting the form. But using AJAX you can fully validate the form without the user experience being compromised; to them it will look like any other client-side validation. There are JQuery plugins to help make AJAX-based validation easier to code, but it isn't too hard to roll one up yourself.
you have to use a serverside language for that: php, perl, python... as javascript (client-side technology) cannot communicate with a mysql server (serverside technology) directly.
In javascript you could use the AJAX methodology if you want to avoid the full page refresh. Try using jquery, (a javascript library that take care of crossbrowser issues and simplifies greatly rich interaction programming) and its $.ajax() function. This has been made really easy through the use of the jquery form plugin.
Most examples of this kind of validation use some kind of asynchronous request back to the server to do the lookup in the database. Either by using XMLHttpRequest or some other Ajax style method.
I'd recommend creating a small URL end-point that you can make a quick HTTP request to sending the email address to check. It can return either true or false in a simple JSON response.

Using jquery, what is the simplest function to post some json data and process a returned json response?

When users click on an element in my webpage, I would like to call a javascript function that reads the values of a few text boxes on the page, wraps their contents as json where the keys are the ids for the text boxes and the values are the contents of each text box, and then posts the resulting json to a url.
I would then like the same function to expect back a json response and call another javascript function with the returned json data.
Question:
What is the best way to write the javascript function to create a json structure from html elements, post the json with jquery, and call another javascript function with the resulting json response from the server?
I use malsup's jQuery form plugin.
http://malsup.com/jquery/form/
As he says himself "The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX."
There are some json related options in $.ajax()
Check them out: http://api.jquery.com/jQuery.ajax/
You can use json2 to marshall the data to json format and send it using ajax. Once you got the response back, you can use the result to call other javascript in your success callback. I would recommend using a decent javascript framework for ajax calls to make things a little bit easy.
Check out this question:
How can I post an array of string to ASP.NET MVC Controller without a form?
The accepted answer is pretty simple. But to make it even more simple, make postData a string, and have the SaveList method take in a single string. I just tried it and it works. Just a single statement on each the client and the server!

Categories