i have following part of code:
<input id="add" class="add" data-amount="150" data-monthly="25" type="checkbox" name="accessory[]" value="Accessory Name"/>
wondering if there any way to post all data in "value", "data-amount" and "data-monthly" together to another script?
If you are writing the value of those attributes onto the page server side, you are better off working off of a key and retrieving the values again server side based on the key. The HTML could be altered resulting in non-trustworthy data being send back.
For example...
<input id="add" class="add" data-amount="150" data-monthly="25" type="checkbox" name="accessory[]" value="1"/>
Where 1 is the id of the data that contains the amount and monthly data.
To get all those values in the checkbox, you can concatenate them together separated by some delimiter and then split the value when processing the form.
<input type="checkbox" name="accessory[]" value="Accessory Name*150*25"/>
This is not an elegant solution. It does what you ask, but you should strongly consider Sohnee's answer as a better method of doing what you want to do.
Data attributes are not submitted with form data.
No there is not, however you can put all the attributes you need in hidden fields which are passed with form.
Related
I have the following input in a form
<input name="email" type="text" id="email"size="50" english="Email address" />
I have a custom tag called english, My question is can I send this as post data and can I recover it on my new page ?
Any help would be much appreciated , Thanks
If you use JavaScript to submit your form, you can read you custom tags' values ad append them to the form data to send. Otherwise, clean HTML form just submits only input tags value.
The best method I can think of right now is to have hidden field with the label as value. Like
<input name="email_label" type="hidden" id="email_label" value="Email address" />
The short answer is: no. The post data received from the HTML in your question will be an array with email as the key, and whatever the user typed as the value.
The solution depends on the problem you're trying to solve. Consider using a hidden input tag instead. For example:
<input name="language" type="hidden" value="English" />
Alternatively, a neater solution would be to store the language in the session (assuming that does what you need). You should never rely on the front end of a website "telling" the back end stuff like this, at least to a certain degree. The back end should just "know".
I have the following query that retrieves client data to create a checkbox group. I need to add the selected checkboxes to a database as individual rows, but I am not sure how to write this as I have not done it before. I'm confused because the number of checkboxes will vary in number.
res=mysql_query('SELECT id,name FROM tbl_client WHERE active="1" ORDER BY name DESC',$dbh) or die(mysql_error());
num=mysql_num_rows($res);
for($run=0; $run<$num; $run++)
{
val=mysql_fetch_row($res);
echo '<label><input type="checkbox" name="CheckboxGroup1" value="'.$val[0].'" id="CheckboxGroup1_0" />'.$val[1].'</label><br />';
}
How do I access the checkboxes in order to add them to individual rows in a database when I submit them? I guess I could use some kind of Foreach statement, I just have no idea how to do it.
What you need is to use an array, which means naming your input fields with trailing opening [ and closing square brackets ] like so...
<label><input type="checkbox" name="CheckboxGroup[]" /></label><br />
What this does is create an array named CheckboxGroup in your $_POST or $_GET super globals (depending on which method the form uses), when the form is submitted to your PHP script. So if you had 1 or 1000 of these input elements in your HTML they will all populate under $_POST['CheckboxGroup'] -- or $_GET -- in PHP and you can iterate over them like this...
foreach ($_POST['CheckboxGroup'] as $checkbox) {
/* do whatever you want with $checkbox here */
}
Please note that a checkbox value is only sent in the request by the UA if it is checked. Also please don't use mysql_* functions to interface with your mysql database in PHP as it is deprecated and will be removed from future versions of PHP. For new applications please consider using either MySQLi or PDO to interface with your mysql database in PHP.
In HTML forms, the checkbox and radio controls are unique in that they are not present in the request if they are not fired by the client. So your script has to know in advance what checkboxes to expect. With inputs of type=text, submit, etc., the request contains the named control, even if the data string is empty.
There are several ways to accomplish this sort of thing. You can make arrays of checkboxes, with or without named elements, like this:'
<input type="checkbox" name="color[orange]" />Orange
<input type="checkbox" name="color[green]" />Green
You can also provide type=hidden form elements with the same names as the checkbox elements. The last form element of the same name is the one that will be presented with the request. By doing this you can always have a predictable number of elements. The hidden value will be in the request if the checkbox is not fired. The checkbox value will be there if the client selected the checkbox.
Try setting up a form that submits your checkbox tests to a PHP script that says this (shown here in its entirety)
<?php var_dump($_POST);
Having looked at various similar questions, both on SO and elsewhere, I have a horrible feeling what I want to do is impossible, but here goes.
I have a page that is a table of text input rows. The user enters information on each row, and submits the data to a separate file, which creates a PDF.
The problem is that I need the user to be able to add rows to the table at will, since the amount of data can vary.
[Before you go there, I need to point out that I cannot use Javascript for any of this - I know it is easy to do in JS but the page needs to be accessible.]
Here is a very simplified version I just cobbled together to (hopefully) illustrate the point:
<?php
if (filter_has_var(INPUT_POST, 'add_rows')) {
$howmanyrows = filter_input(INPUT_POST, 'howmanyrows', FILTER_SANITIZE_NUMBER_INT);
//get all the data from table and put it in an array,
//then add 5 (or however many) new rows to said array.
}
else if (filter_has_var(INPUT_POST, 'send_data')) {
//get table data, add to session and redirect to other page with a header()
}
?>
<html>
<form action="" method="POST">
<table>
<?php //table rows added using an array of data
foreach ($data as $d): ?>
<tr><td><input type="text" value="<?php echo $d; ?>"></td></tr>
<?php endforeach; ?>
</table>
<input type="text" name="howmanyrows" value="5">
<input type="submit" name="add_rows">
<input type="submit" name="send_data">
</form>
...
</html>
As you can see, at the moment I have a clunky setup where there is just one form that encompasses the entire page, and submits the page to itself. Depending on the button that was clicked, a new row is added or the data is submitted to the PDF-creation page.
This is not ideal, for so many reasons. What I really want to be able to do is have two separate forms, or nested forms. But the former won't allow the input values to be submitted to both, and the latter is apparently bad form (no pun intended) and doesn't work.
Is it at all possible to make this do what I want it to do? Any suggestions for a different way to go about it?
I think you have the best non-javascript solution - certainly hte way I'd run with it.
One thing to make it easier is that you can use multiple inputs with the same name:
<input name="tablerow[]" type="text" value="A" />
<input name="tablerow[]" type="text" value="B" />
<input name="tablerow[]" type="text" value="C" />
And these come through the $_POST['tablerow'] as an array. The length of the array is the number of fields. Then add additional fields to that.
For accessibility, you should add a link at the top that allows the user to hop directly to the first "new" field - otherwise they need to tab through the entire form to get to the new field. (See my comment above about if JS is really unavoidable as you and they can avoid this scenario!)
How can I submit a form to itself without clearing the data in the fields using HTML, javascript and PHP?
You could take different approaches (e.g. cookies, jquery, etc...), however HTML + a line in PHP are more than enough in this case. Try this example code:
<form name="test" method="post">
Your Name: <input type="text" name="YourName" <?php if (isset($_POST['YourName'])) echo 'value="'.$_POST['YourName'].'"';?> >
<input type="submit" value="Submit">
</form>
In the code above if something has been posted to the receiving page (that can be the same page, such as in your case), then the posted value is printed out in the corresponding field. You can use this approach for all the fields composing your form.
If you want, you can also use similarly the $_GET method in the form.
If you use the traditional form submit, you need to save the parameters and rewrite the form input elements when you write the form the next time. But a better way is to use AJAX -- then the field data is sent without a form submission, and the input elements retain their data. See this link: http://www.w3schools.com/ajax/default.asp
I have a index.html where I would like to submit some coordinates that can be passed upon to separate PHP file; where it could perform a query. I am new to this.
HTML:
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
PHP query:
$query = "SELECT * FROM state WHERE LONG_HI<$_POST["Ymax"] AND LAT_HI<$_POST["Xmax"];
$result = mysql_query($query);
So is there a way to perform remote action from this HTML file to the specified PHP file?
Well, Forms can do the job. Is'nt it?
Yes
Either make an HTML form to accept the Xmax and Ymax parameters, and set the form action to the PHP file;
Or use AJAX to pass the data in the background and receive a response.
If both of these concepts are foreign to you, and you don't know JavaScript, get comfortable with the first option first.
Would you please describe in detail what you are about to do?
do you have a html form?
What kind of request do you do, clicking a link, sending the form?
The query does not contain any of the variables...
could you please post excerpts of the code? single lines are useless in most cases.
Regards,
Mario
use action attribute in FORM element to specify where the request will be sent to.
<form action="another.php" method="POST">
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
</form>
You just add few line with your code because to transfer any variable value from one form to another page we have to use 'form' method. So, we have to add form tag with your code. Transferring of data from one page to another page (any type of page like php, jsp, aspx etc) is done by two methods mainly - one of them is Post and another one is Get.
Difference between both the method is quite simple. In Post method, data from one page to another page travels in hidden form whereas Get is basically used to transfer value by displaying it at url. Post method example: user-name and password, and Get Method: any query fired at Search Engine.
<form name="form" action="filename.php" method="POST" >
//Your Code
</form>