I'm having a tough time figuring out how to do something which I think should be pretty basic but I just can't seem to find an answer that makes sense to my basic understanding of jquery.
Question 1:
I'm trying to generate a checkbox input that returns a value of 0 if left unchecked and returns a value of 1 if checked.
Here's how I figure I'd generate the checkbox:
<input type='checkbox' id='part_owner' name='owner/>
Here's how I figure I'd send the value:
item.find("input[name=owner]").attr("value",p.owner);
Question 2:
How would I retrieve the value, once saved to my database, and tell my checkbox to display accordingly?
Here's vaguely how I figure it would work:
owner: $("#dialog input[name=owner]").attr("value"),
Where owner is the field name being sent retrieved from the database via php.
Anyway, this question has been the bane of my existence for a duration that's slightly embarrassing. An explanation of this would be greatly appreciated, as I've apparently been having a hard time connecting the dots on this one.
Thanks in advance!
There are two options:
1. In HTML make one input:
<input type='checkbox' id='part_owner' name='owner' value="1" />
and in PHP make check:
if (!isset($_POST['owner'])) {
$_POST['owner'] = 0;
}
2. In HTML make just two inputs in exactly this order:
<input type='hidden' name='owner' value="0" />
<input type='checkbox' id='part_owner' name='owner' value="1" />
If checkbox will be checked, form will send value 1. Otherwise 0 will be sent.
When you use jQuery, you can detect if input is checked by:
if ($('#part_owner').is(':checked')) ...
Or check it with:
$('#part_owner').attr('checked', true);
Related
I am processing an html form using php. My question is specifically about input type checkbox:
<input type="checkbox" name="checkme" value="checked" <***php echo $data['checkme']; ***> >
This works for me because, on load, $data['checkme'] = ""; and on error, $data['checkme'] = "checked". I have searched quite a bit regarding this, and there are plenty of suggestions for setting the value of the checkbox input. But not in this way (that I found). This works, but I want to make sure I am not creating a problem that I don't foresee.
My question: Is this good practice?
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;
?>
<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
This works fine, however when you click on submit, and the checkbox is ticked, it passes BOTH the hidden value and the original checkbox value to the $_POST variable in php, can this be avoided?
I have the hidden value there, so that unticked checkboxes are passed to the $_POST variable as well as the ticked ones.
The better approach is to remove the hidden field, and simply have a check in PHP:
if ($_POST['check_box_1']=='1') { /*Do something for ticked*/ }
else { /*Do something for unticked*/ }
You shouldn't need the hidden field. You should in fact not trust any of the form fields sent in the first place. What this means is that you cannot make code which takes the sent fields and trust them to send the correct data (which I assume you do now).
What you should do is to handle all fields you expect to get. That way if you don't get the checkbox value you can still handle that as if it was unticked. Then you also get the added inherent feature of throwing away form data you don't expect in the first place.
No, it will pass all the form data, whatever it is. The right way to do this is not to set the checkbox via a hidden field but to set the checkbox with whatever its state actually is!
I mean... why are you adding the hidden field to begin with?
Your PHP is receiving two fields named check_box_1, and last time I checked there was no way to guarantee that the params would get read into the REQUEST hash in the exact same order as you sent them, so, there's no way to tell which one will arrive last (that's the one whose value will get set). So... this is not the right approach to whatever problem you're trying to solve here.
Welcome to Stack, btw! If you find answers useful or helpful, make sure to mark them as correct and vote them up.
That's normal.
They must be both type="checkbox" to pass only 1 value.
If you want to get only 1 in any cases you can do:
<input type="checkbox" style="display:none;" name="check_box_1" value="0">
Make sure the first input field is of type Checkbox, or else it won't behave like one.
<input type="checkbox" name="check_box_0" value="0" />
<input type="checkbox" name="check_box_1" value="1" />
Everything is working normal with your code so far.
I'm assuming you are creating the hidden field so that 0 is passed to the server when the checkbox is not checked. The problem is that they both get passed when the check box is checked.
As Death said, the way you should be doing it is with a single checkbox and then checking if the value has been sent to the server or not. That's just how checkboxes work.
If you want to have a default set then you will have to handle all that on the server side based on weather the checkbox has a value.
For example:
$myValue = "";
if(isset($_POST['check_box_1']))
{
$myValue=$_POST['check_box_1'];
}
else
{
$myValue="0";
}
Is it possible in php to include a forms value into the action redirection?
For example:
<form method='POST' name='Select' action='customer.php?CID=xxxxx'>
<input type=text width='5' name='searchVal' />
where xxxxx is the value entered into the form.
I've tried a number of different ways and I'm just not figuring it out! (Still sort of new to php) Any help would be appreciated.
It was looking like I would have to use $_POST and $_GET. A little more information might be in order... customer.php displays a list of customers in order by ID, name, etc. The user currently clicks on the customer ID that they want to display the details for. I'm trying to add a box where they can just enter the customer number to get to the details quickly, but I still want to have the listing displayed. From what it is sounding like, I will have to do this as two separate programs...is that true?
How about this:
<form method='POST' name='Select' action='customer.php'>
<input type='hidden' value='xxxxx' name='CID' />
<input type=text width='5' name='searchVal' />
...
</form>
You are free to add as much hidden values as needed.
Note, that you can even use PHP-like array notation_
<input type='hidden' value='xxxxx' name='CID[1]' />
<input type='hidden' value='yyyyy' name='CID[2]' />
At the PHP-side, access those values using this syntax:
$_POST[ 'CID' ][ 1 ]
$_POST[ 'CID' ][ 2 ]
UPDATE-1
Ah, you want to use a user-entered values to the Action URL just before the form gets submitted?
In this case you need to use JavaScript. Access the DOM to change the Action URL.
But let me ask, why you need to post a form value additionally as a parameter of the Action URL?
UPDATE-2
You wrote: 'From what it is sounding like, I will have to do this as two separate programs...is that true?'
No, actually not. You can still use one customer.php which checks at its beginning, if it was called using a linked customer in the table element or a searched customer in the search field.
In other words: You don't need to prepare two scripts, but two forms for two purposes which call the same script customer.php.
You can include the required value in a hidden field in your form:
<input type="hidden" name="CID" value="xxxxx" />
The reason this is required is that you are submitting the form to your server via POST, but appending parameters to the URL requires submission via the GET method.
Not without a post to the server. The value in the form is filled in client-side, so it has to return to the server before you can add it to the action. (at least, if you want to use php).
You can either
add it after posting (might not be usefull)
use javascript
just not use the GET CID, but get it out of the POST in your customer.php script.
I got it finally! It's actually very simple!
In the body of the code I put this:
<form action='#_SELF' method='GET' name='Projected'>
<input type=text size=5 name='CID' value='' title='Enter Customer number to display' />
<a href='#' onclick='document.Projected.submit();' title='Enter Customer number to display'>Go</a>
And at the top of the code I just do a:
if (!isset($_GET['CID'])) { ...
It works exactly the way I wanted it to!
Thanks everyone for the help! I appreciate it! (And I'm learning more and more about PHP everyday!)
Im pretty sure you cant do that unfortunately
what i want to do is that when you vote Y or N (two different radio buttons) and then it inserts into the "vote" column(in database) = Y or N(what you pickd), if nothing then echo error.
I know how to do this like halfway, i never worked with radiobuttons before so i need you guys.
Here's a two radio button right:
Yes: <input type="radio" value="Y" id="voteYes" name="vote"></input> <br>
No: <input type="radio" value="N" id="voteNo" name="vote"> </input>
I gave the value N and Y, not the same ID, but the same name. I think its right, but how should i do with the PHP part of what i want to do? I mean shall i call for "vote"? ($_GET["vote"]) i dont think so.. here's where im stuck
You're almost there. Depending on whether or not your form uses the GET or POST method, you'll have to change the variable name appropriately.
if(isset($_GET["vote"])) //checks to see if the user inputed something
{
$value = $_GET["vote"]; //remember, this value is not guaranteed to be either Y or N
}
else
{
//display your error
}
Why not? Just make sure you validate your data... make sure $_GET['vote'] must be an element of array('Y', 'N'), if true you insert it, else echo error and you're done.
POST variables are found in $_POST.
echo $_POST['vote'];
It depends on the method used. If you POST the form, then the variable will be $_POST['vote'], if you GET the form (default) then it will be $_GET['vote'].
Specify the method used in the form tag:
<form action="foo.php" method="POST">
or...
<form action="foo.php" method="GET">
Check for the existance of the variable either with isset() or array_key_exists().