insert and edit with checkbox coding - php

I am a newible on php and I am following a video to learn inserting data in website to database, but I face a huge problem.
As I am going to from many groups from many members, i create some checkboxs for member to register.
I followed this video tutorial,but have error.
<?php
//$_SESSION['name'];
session_start();
echo "student";
?>
<form action="" method="POST">
<input type="checkbox" name="group[]" value="group1">group1<br>
<input type="checkbox" name="group[]" value="group2">group2<br>
<input type="checkbox" name="group[]" value="group3">group3<br>
<input type="checkbox" name="group[]" value="group4">group4<br>
<input type="checkbox" name="group[]" value="group5">group5<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit']))
{
//print_r($_POST);
echo implode(',', $POST['group']);
}
?>
</h1>
Logout
All things that I wanna do was followed for these coding but still have error
Warning: implode(): Invalid arguments passed in
C:\xampp\htdocs\fyp\student.php on line 37
echo implode(',', $POST['group']);
what was wrong with it, thank you for any suggestion

You are missing $_ before POST .
Instead of this
echo implode(',', $POST['group']);
use this
echo implode(',', $_POST['group']);

You're forgetting the underscore before the POST. Here is that line fixed:
echo implode(',', $_POST['group']);
There are a few other simple issues that might cause some trouble later on. First, in terms of syntax, you should close your HTML input tags (like below):
<input type="checkbox" name="group[]" value="group1" />group1<br>
...
Next, you will get this same error when no checkboxes are selected as the variable $_POST['group'] has not been defined yet. You can use the PHP empty() function to test this variable (http://php.net/manual/en/function.empty.php) before you call the implode().

Related

Notice: Array to string conversion in line 6! Problem about SQL

I have a simple code (very simple one) that I was using to try something out for a work and I was trying a function to work with the variables of a form radio in post method, to update my SQL table with the output of the form. But when I'm going to try it, it doesn't update and gives me a notice.
It has something to do with the query (because the error says is in that line of the code) but I still don't know what it is.
I tried to change the syntax of the SQL sentence in different ways. I changed the user I was going to use to change the "image_value" column. I even checked the syntax of the query in phpmyadmin, and it worked.
Here is the php code:
<?php
mysql_connect("localhost","root","");
function user_image($value){
print_r($value);
//This is the problem
$query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'";
mysql_query($query);
}
?>
And here i have the code of the form and how I'm using the function (if there is any mistake that I haven't seen)
<form method="post" action="">
<input type="radio" name="1" value="1">imagen1
<br>
<input type="radio" name="2" value="2">imagen2
<br>
<input type="radio" name="3" value="3">imagen3
<br>
<input type="radio" name="4" value="4">imagen4
<br>
<button type="submit"><span>Submit</span></button>
</form>
<?php
user_image($_POST);
?>
Your problem is you are passing the complete object $_POST.
You don't specify if your radio name is correct (The name of your radio as a number from 1 to 4).
In the case, you are trying to set the value of image_value from a radio button should be.
<form method="post" action="">
<input type="radio" name="image_value" value="1">imagen1
<br>
<input type="radio" name="image_value" value="2">imagen2
<br>
<input type="radio" name="image_value" value="3">imagen3
<br>
<input type="radio" name="image_value" value="4">imagen4
<br>
<button type="submit"><span>Submit</span></button>
</form>
<?php
if (isset($_POST['image_value'])) {
user_image($_POST['image_value']);
}
?>
and your function
function user_image($value) {
mysql_connect("localhost","root","");
print_r($value);
//This is the problem
$query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'"; //ID should be dynamic base on the user I guess
mysql_query($query);
}
?>
$_POST pass an object value to the server, you need to specify the property you want to use, var_dump $value to understand all what it contains.

Form with checkboxes getting all checked checkboxes

I am trying to make a random tournament generator, where I can select names from a list with checkboxes and then randominze them into a different order.
I have the following form:
<form method="post" action="<?php echo ROOT ?>HomeController/createTournament/" enctype="multipart/form-data">
<div class="form-group">
<label for="participants">Select participants</label><br>
<?php foreach($players as $p): ?>
<input type="checkbox" name="participants" value="<?php echo $p['name'];?>"> <?php echo $p['name'];?><br>
<?php endforeach; ?>
</div>
<button type="submit" class="btn btn-primary btn-block" name="create">Show participants</button>
</form>
This form show's a checkbox and behind the checkbox the name of the participant.
This is my method:
public function createTournament() {
if(isset($_POST["create"])) {
$participants = $_POST['participants'];
}
include('app/views/showTournament.php');
}
That means I am saving the checked ones into $participants, right?
In the file showTournament, I know have access to $partipants.
I try to var_dump $particpants and it shows me:
string(6) "Onlyoneselected name"
So I tried a foreach, to get ALL of the selected names.
<?php
foreach($participants as $p) {
echo $p;
}
;?>
The foreach isn't showing anything, but the file has access to $participants. I want all the names on my screen, so I can start randomizing them. What do I do wrong?
<input type="checkbox" name="participants"
This line here is the root of your problems.
Because every checkbox has the same name, the value of $_POST['participants'] gets overridden for each checkbox in the list.
If you change that snippet to:
<input type="checkbox" name="participants[]"
Then $_POST['participants'] becomes an array of all checked values.
You need multiple checkbox values.
And therefore, HTML name of the input should be multiple (array)
<input type="checkbox" name="participants" will return string, only latest submitted value.
<input type="checkbox" name="participants[]" will return array of all submitted values.
So, replacing name="participants" to name="participants[]" will work.

Can't use a form's value in a different php file

Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.

$_GET is not working with radio buttons

Does anyone know why this code will not work? I simply want to print which radio button is selected. It always prints 'Null' no matter what is selected. PHP code is below.
<?php
$conn = mysql_connect('localhost','student','student') or die(mysql_error());
mysql_select_db('vgs',$conn);
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
//Process Question 1
echo "".$q1option;
The HTML code is below.
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
Any help with this would be greatly appreciated. Note I have many tables in the 'vgs' database, if that makes a difference.
Thank you,
Daniel
Additional Code
HTML below
<input type="button" value="Submit" onclick="result();" />
<input type="reset" value="Reset" />
</form>
Embedded JavaScript below. It uses http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
function result()
{
$('#Suggestion').load('process_answers.php');
}
Looks like your submit button is outside of the <form> tag, it should be within the form:
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
<input type="submit" value="Submit" />
</form>
You are always getting Null in return because your query string is not being attached anywhere. If you are using AJAX, you need to pass the query string data. Your cycle is not working properly because of a misconfiguration.
Take the following code:
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
Using $_GET will attempt to extract from your query string a variable labeled 'q1option'. If this variable is not set, then return 'Null'. So your code is working as expected as it is currently structured.
If you want to pass the variable between pages you will either need to submit the form and the variable will be automatically passed, or you need to modify your jQuery to allow for the passing and processing behind the scenes of your variable. Once processed, you would then handle the results accordingly and render them to the client as desired.

kohana parse $_POST data

i have a kohana application, and i have a form with several checkboxes, and the user is supposed to check his preferences there in the form. so i have a relation 1:n between the user table and the preferences table. my problem is that i want to save those preferences, selected in the form, and i don;t know how.
i have the form:
<form id="address" method="POST" action="<?= Route::url('Save user preferences' , array('user_id' => $user));?>">
<? foreach ($prefered_products as $pp): ?>
<input type="checkbox" name="user_preferences_preference[]" value="<?= $pp ?>" /><?= $pp->product; ?><br />
<? endforeach; ?>
<button type="submit">Salveaza preferintele tale</button>
</form>
and i save the data:
foreach ($_POST['user_preferences_preference'] as $up) {
$user_preferences->prefered = $up;
$user_preferences->user = $this->user;
$user_preferences->save();
}
$this->view->message = __('Thank you for your feedback!');
but seems like the way i parse the preferences is not correct, i am getting: ErrorException [ Warning ]: Invalid argument supplied for foreach()
any idea about how am i supposed to get the multiple $_post preferences?
thank you!
I have a slightly different way of doing this.
When I create a checkbox I also create an identical hidden field set to zero
<input type="hidden" name="my_check" value="0" />
<input type="checkbox" name="my_check" value="$value" />
The checkbox, if ticked, will override the hidden value. This way when you send the form you end up with $_POST['checkbox]=1 or 0, but it always exists in the $_POST.
The nice thing about this method is you can extend the Form::checkbox helper so that it's always present and you don't have to worry about it for every form / controller.
p.s. in you above example you would probably want to do it like this:
<input type="hidden" name="user_preferences_preference[$pp->id]" value="0" />
<input type="checkbox" name="user_preferences_preference[$pp->id]" value="<?= $pp ?>" />
<?= $pp->product; ?><br />
Or use a $key value instead of $pp->id.
The problem is that a checkbox will only post data when set. You should reverse check the values. Ie;
Fetch all preference (id's) from the database
Check if a value is found in the $_POST var
If not, update to false (or 0 or whatever) in db, if set, read out the value.

Categories