HTML Button contain two names - php

I am trying to make the button in my form contain two names. Let me explain.
I have 2 buttons, the first one has a value of 1, and the other one has a value of 2. They are both connected to the post method "Click". But I need my "$pos = $_POST['position']" to become the value of the button. Is that possible?
What I tried was <button name="click" name="position" type="submit"> but it's not working

No element can have two attributes with the same name (i.e. you cannot have two attributes named name on one element). Some attributes take a space-separated list of values, but name is not one of them.
Pick either click or position to be the name of the button, and change your server-side code so that it recognises data with that name as representing both pieces of information that you were trying to convey.
You'll probably want to add a value attribute too.

Related

PHP- Get index/associated value of option selected in dropdown box

I want to show values in a dropdown box (either from an array or database - please advise on which option is method)
Then
when a user selects a value in dropdown box, i want to have and get its associated index value (like 1, 2).
For example: dropdown box shows values:
"Car"
"Bicycle"
If user selects "Car", when i get dropdown selected value, i should get 1, similarly for "Bicycle" i get 2 .. and so on.
Please advise easy and simplest method to implement this
Thanks
You can use two methods:
1) Set up prior to display a code that holds associative array with key -> value i.e: 1 => Car (you can keep it in config file if it doesn't change frequently, you can pull it from database or you can keep it in some other form: serialized, file etc.) and use it when the submitted form is being processed.
2) Use array with key and value with the same string i.e: Car => Car and when the form is processed you will have value right away. This solution has some limitations and be troublesome with using more words or other characters that need to be sanitized.
I would advise option 1, you will have to set up the list before and use it after form has been processed but it allows more freedom and its less maintenance.

Can I reliable count on the order of GET variables with the same name?

I have a form that is dynamically created on the client side. It resembles a library search form (see image of example). From a Javascript/Client-Side point of view, it would be hard to have each row rename its self with a number attached to its name (so I would now which elements were in the same row, and in what order the rows are). Can I trust that the form will be passed in order? As in, the first item with name=type and the first item with name=value will have been in the first row...
Image (Note that this is not my actual form, but notice how you can dynamically add rows in this example):
I'm using GET, by the way.
Don't. If you suffix the name with [] then it will create an array in $_GET/$_POST under the name before the brackets in the same order that they appear in the form. You can then use normal array operations to get/zip the values from the arrays.

Fill form with checkbox array

I want to create a PHP script which can fill up a form from a text file. The form is on a password-protected page. There are 4 fields where i need to put text (name, village, etc.)
I need to select checkboxes but they are in an array and I only know their label name. At max I can look up id's and use it as static values, but I don't know them how to use them either.
<form method="POST" action="...">
<input type="checkbox" name="tag_id[]" id="tag_id_192" value="192"><label for="tag_id_192">House</label>
I saw lots of tutorials which shows how to do it with checkboxes but they used the check box name and without the login part. I never used PHP before so I don't know even where to start. If someone could guide i would be very grateful.
Example:
The .txt file each row represents a form fill up the fields are separated with a *: Name * village .... * the check boxes label names which i need to set active
If you're using cURL, you can post multiple fields with the same name, if it has [] after the name, php will automatically concatenate them all into an array..
So its valid to do tag_id[]=192&tag_id[]=175&tag_id=[285]..
In PHP it will know to make it array(192,175,285)
As a side note, fields that do not have [] get overwritten each time they appear...

HTML form with multiple submit options

I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for transfering data.
For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.
However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).
Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?
The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.
<input type="submit" name="delete[1]" value="delete">
if (isset($_POST['delete'])) $id=key($_POST['delete']);
it seems to submit always just the last ID
It submits all of them, but since the name doesn't end with [], PHP discards all by the last.
Is there any way to submit just the ID field of the corresponding item without using multiple forms?
No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.
If I understand your goals correctly, you have two main options.
Put one form per row (in the cell with the delete button)
Encode the id value into the name of the submit button
You could get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}

Array count values

I am new to php, i have some problem in php page. This page contain one while loop, in that loop itself taking record from data base, it showing taxt box, list box like this,,,etc..Here php page while loop contain more then 5 records. here i need to select list box value to pass post to set session in next page. From that session i have take value to show next php page.
So here which type array i have to use,...and using get am going to pass value ,,,but value is 0 coming without selecting value,,..if am select list box value it ll come null value,,please suggest how can i pass whole value to next page,...
thanks
periyasamy
Insert the contents of the data from the previous page into the form that posts to the next page, in the form of <input type="hidden" name="foo" value="bar" />. Then, pull it out of the $_POST array on the next page.
About the <select> element returning NULLs, try specifying the name attribute on the <select> element, and a value attribute on each of the <option> elements. Don't forget to have one with the selected="selected" attribute set, to specify a default.

Categories