radio value lost by $_post array - php

I have this simple form:
<form method="post" action="?step=2">
<label>A4 - Colour / Colour
<input type="radio" name="leaflet" value="1"></label><br>
<label>A5 - Colour / Black
<input type="radio" name="leaflet" value="2"></label><br>
<input type="submit" name="leaflet" value="Select">
</form>
When I apply print_r ($_POST); to the submission though, I only get the submit button data. I don't even see the radio name.
What could do that?

PHP's standard form parsing system (which populates $_POST) can't handle multiple bits of form data with the same name (unless that name ends with the characters []).
Change the name of the submit button.

Buddy you have same name for submit button and radio button :)
ie leaflet
The value get overridden. I HOPE u got it

Related

how to get the id of input type imagein in php

my form has images as inputs so i used
there is no submit input because i used the image as submit
now i can get the image id
<input type="image" src="my/source" name="Logo" />
i had tried to put hidden input but it returned all the ID's in the data not the selected one
If you need to transport an actual value, then <input type="image"> is not a good choice to begin with. The only info you will get in the form submission data set, is the click coordinates, under the parameter names Logo.x and Logo.y (with name="Logo".)
You should use a button instead. That allows you to specify the submission value, and the image itself can be put inside the button, using a normal img element.
<button name="Logo" value="some_id_here"><img src="my/source"></button>
type="submit" is the default type for buttons, so it does not need to be put on there explicitly to make this a submit button.

Set a SESSION by clicking a radio button in PHP?

I have a Form on my site and part of the Form allows the user to select a colour of car.
I'd like it so once they select a radio button, it updates a session variable dynamically - using Ajax & PHP.
HTML Form:
<form method="post">
red: <input type="radio" name="car" value="red"><br />
blue: <input type="radio" name="car" value="blue">
</form>
PHP Script:
session_start();
$carColour = $_POST["car"] ;
but I'm not sure how to do this with Ajax so it doesn't leave the page.
Is this possible - and how would I achieve this?
Many thanks for any help.
You wouldn’t be able to do it with PHP alone, as it’s not an event-driven language. By the time you click the radio button, the PHP interpreter has been and gone.
You’ll need to use JavaScript function to call a PHP script that sets the session, and attach that function to the onclick event of your radio button.

How can I know which option is selected in an HTML form?

I want to have a multi-step form with HTML and PHP.
The first step of my form is an option like:
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
Now, my question is: how can I know which option is selected so that I arrange the next step options for the user?
For example: If the user chooses option 1, next step would be: "You have chosen option 1, tell me who's your daddy". And if the user chooses option 2, next step says: "Welcome to option 2, tell me what you like", etc.
Now, I'm a totally beginner in PHP/HTML and know nothing about javascript. If you're answering this, I'd be so thankful, but please do it in an easy-to-understand sort of way.
I have already found this related to my case, but it is very hard to customize, and the validation process is of before CSS3.
[edit:]
Now I want to add a text-type input like this:
<input type="text" name="fname" value="firstname">
The guys told me to use $_POST['fname'] but for input texts, the 'value' property will show up inside the textbox like a default caption. I don't want this.
Now what do you suggest?
the the value from $_REQUEST:
$step = $_REQUEST['service_type']; // plan1 or plan2
In your PHP code, use the $_GET (or $_POST or `$_REQUEST - which gets either a GET or POST form) to return the value:
$serveiceType=$_REQUEST['service_type'];
As this is a radio button, only one value can be sent, and the sent value is easily accessible.
At first your input must be in a form tag. Now you can submit the form with an submit button(Input tag with type="submit").
In php you get the results with $_POST or $_GET.
<form method="POST">
<input type="radio" name="service_type" value="plan1"> Plan 1<br />
<input type="radio" name="service_type" value="plan2"> Plan 2
<input type="submit" />
</form>
<?php
$value = $_POST['service_type'];
echo $value;
?>

How can I include a form field in my POST if it is not in the current form?

I have a page that displays a photo the user has uploaded. I have two forms that provide two different actions. The first form, replace_photo_form, is simply a button that the user pushes to replace the photo. The second form, next_page_form, is a field to enter the caption for the photo as well as a button to proceed to the next page. When the user pushes the next_page button, it should save the caption to the DB and continue to the next page.
<body>
<form id="replace_photo_form">
<input type="text" name="caption" />
<input type="submit" name="replace_photo" value="Replace Photo" />
</form>
<div>
<p>This is where some other information is located</p>
</div>
<form id="next_page_form">
<input type="submit" name="next_page" value="Next Page" />
</form>
</body>
I want the caption field to appear directly next to the replace_photo button in the page structure so I included it in the replace_photo_form. The problem is that when I push the next_page button, it doesn't save the POST value for the caption input field as I would like. Ideally I would just include the caption field in the next_page_form so I saves the caption as a POST value, but I need it to appear next to the replace photo button.
How can I include a form field in my POST if it is not in the current form?
Merge the two forms into one that spans both submit buttons (it will still include the input of course).
Then, when the form is submitted, you can check which of the values (next_page or replace_photo) exists in $_POST, therefore discovering which button was pressed and what action you need to take.
This technique will work correctly even if Javascript is disabled. If you are willing to relax this restriction, there are dozens of other options (e.g. hooking the submit event of a form to copy the value of the text box from another form in a hidden field of the current form).
One possibility is to add a listener to the field that ties its value to a hidden field in the second form:
<form id="replace_photo_form">
<input type="text" name="caption"
onchange="document.getElementById('next_page_form_caption').value = this.value;"/>
..
</form
<form id="next_page_form">
<input type="hidden" id="next_page_form_caption" name="caption"/>
..
</form>
You mean replace_photo_form submit before next_page button clicked, you can simply add an onclick to next_page button, like this:
<input type="submit" name="next_page" value="Next Page" onclick="document.getElementById("replace_photo_form").submit();" />

How to Submit two values with only one submit button?

Hey im making a website were you can send other users emails.
My problem is when I go and submit it doesn't submit both values?
my Form has a text box where the user wants to send the email and another is a text area which is the message.
and i don't want 2 submit buttons
Have a look at http://www.w3.org/TR/html401/interact/forms.html#h-17.1 on how to create forms.
Your inputs need unique name attributes.
Example:
<form>
<input type="text" name="recipient_address" />
<textarea name="message_body"></textarea>
<!-- no real need for a name on the submit button -->
<input type="submit" value="Send Message" />
</form>
any value that you want to send to the server make sure it is in the form tag. When the user clicks your "submit" button all of the data in the form is sent off along with the page request. You can have as many items as you want in the form.
<html>
<head>
/*... your code here
...*/
</head>
<body>
<form name='sampleForm' action='sample.htm' onsubmit='ValidatethisForm()' method="post" enctype="multipart/form-data">
<input type='text' name='formElem1'>
<textarea name='formElem2'></textarea>
<input type = 'submit' value='submit'>
</form>
</body>
</html>
In the validate function once u finish your validations use "return true" to proceed. You can submit multiple form elements as long as they have different name attributes and the are within your form tag.
By far the best I have found is here
Happy Coding
Check your input and add a unique name attribute.

Categories