php program getting value from radio in while loop - php

hi guys im just trying to get the value of selected radio button but it doesnt work help me guys pls. how can i get the selected radio button value tnx in advance :)
<html>
<head>
<title>asdad</title>
</head>
<body>
<form method = "POST">
<?php
$con=mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('aclc_esys') or die("cannot select DB");
session_start();
$query=mysql_query("SELECT * FROM add_student_subject WHERE student_id = '".$_SESSION['sess_stud_id']."'");
while($row=mysql_fetch_array($query)){
?>
<input type="radio" name="subj" value="<?php$roww['subject_name']?>">
<?php
echo $row['subject_name']."<br />";
}
?>
<input type="submit" value="Submit Evaluation" name="submiteval" />
<?php
if(isset($_POST["submiteval"])){
echo $_POST['subj'];
}
?>
</form>
</body>
</html>

You have misspelled row and you forgot to echo.
Instead of:
<?php$roww['subject_name']?>
Try:
<?php echo $row['subject_name']; ?>
Also give at form element an id attribute.
It is very important to use mysqli_* functions not mysql_* ones (they are deprecated) or even better PDO. At least sanitize and validate your inputs so your data will be as valid as you can.

Related

drop down menu php with condition

I have a code which has a form that inputs surface area. db_connect.php connects the database. I am trying to populate a drop down list with a condition that all values that have surface area greater than the value typed into the text field will be displayed in the text field. But when I try to run the code, i'm getting all the values. How can I solve this? Thank you in advance!
<html>
<head>
<title>hi</title>
</head>
<body>
<form>
<p> surface area : <input name = "sa" type = "text"> </p>
<br>
</form>
<select name="areas">
<?php
$sa = $_POST['sa'];
include "db_connect.php";
$displayArea = "SELECT area FROM details where area > '".$sa."'" ;
$sql = mysqli_query($link, $displayArea);
echo "<option> Select </option>";
while ($row = mysqli_fetch_assoc($sql))
{
echo "<option value=\"areas\">" . $row['area'] . "</option>";
}
?>
</select>
</body>
</html>
first you need a submit button into the form.
<input type="submit" value="Submit">
Then if you are using POST you have to specify it as a Form method:
<form method="post">
Then add:
$sa = $_POST['sa'];
echo("[".$sa."]");
to see if "sa" is populated.
If you add a value and click on "Submit" you will see the result.

I want to delete a selected item that is in combobox using PhP and that must also reflect in Database

This is the php code to delete.
<?php
$namez = $_POST['vendel'];
$name = mysql_real_escape_string($namez);
mysql_connect("localhost","root",
"") or die("Wrong username or password");
mysql_select_db("vendor") or die( "Unable to select database");
mysql_query("DELETE FROM vendoradd WHERE venname='$name'");
?>
And here is the code where I select from a combobox.
<form action="delete.php" method="post">
<select name="vendel">
<?php
$con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
$query = mysqli_query($con,"select venname from vendoradd");
while ($row=$query->fetch_assoc()){
echo "<option value=\"vendel1\">".$row['venname']."</option>";
}
?>
</select>
<input type="submit" value="Delete" id="del">
</form>
I am not getting any error, the page is redirecting to delete.php file but when I cross check the database the selected data is not deleted. Any help is appreciated. Thanks in advance
Well... Its because you're posting a value "vendel1", which means that you're always sending the same value, just for you to know... The name of the select combobox is the index in the $_POST and the value in the option is the value, for example, if you have:
<form action="" method="post">
<select name="foo">
<option value="1">Stack</option>
<option value="2">Overflow</option>
</select>
<input type="submit">
</form>
<?php
echo $_POST['foo'];
?>
And you select the option "Stack" the output of
$_POST['foo']
will be 1.
So if you re going to post with a select combobox, you'll have to give an unique value to each option, so you can delete the correct one in your database. My recommendation for you is to create an unique ID to each user in your table, its better to compare unique ID's than strings... Why? Because if you have two people with the same name, you'll delete both, but, with an unique ID you'll not have that problem. If you want to delete
<form action="delete.php" method="post">
<select name="vendel">
<?php
$con=mysqli_connect("localhost","root","","vendor") or die("Unable to connect");
$query = mysqli_query($con,"select TRIM(venname) from vendoradd");
while ($row=$query->fetch_assoc()){
echo "<option value=".$row['venname'].">".$row['venname']."</option>";
}
?>
</select>
<input type="submit" value="Delete" id="del"></form>
And your delete should be like this:
<?php
$namez = $_POST['vendel'];
$name = mysql_real_escape_string($namez);
mysql_connect("localhost","root", "") or die("Wrong username or password");
mysql_select_db("vendor") or die( "Unable to select database");
mysql_query("DELETE FROM vendoradd WHERE TRIM(venname) = TRIM('$name')");
?>

Array of forms in PHP

First, thanks for taking a look at this. I am trying to create an array of forms that acts as a dynamically sized results list. From the results that were given the user can click 'detail' (a submit button) to get further information on the result which is why I am attempting to create an array of forms. Here is what I had tried, which compiled but the buttons aren't doing anything. Any help would be great :)
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$numbers=array(1,2,3,4,5);
$listsize=count($numbers);
for($currentnum=0;$currentnum <$listsize;$currentnum ++){
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
</form>
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
</body>
</html>
This is really just meant to demonstrate what I am trying to do (thought that would be easier without functions out of scope of question).
Try changing the HTML for the button:
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
Should be:
<input type="submit" value="Submit" name="button<?PHP echo $currentnum?>" />
Close bracket for your for loop so that your check is inside it:
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
Should be:
<?php
echo "<br/>";
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
}
?>
If you learn to indent your code you'll find these kinds of bugs much easier to spot!
Other than that you're good to go...
You need to change your input types from 'button' to 'submit' so that they submit the forms, then you need to move
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
inside of the for loop

How do I retrieve a selected value from a dynamically selection box

I would like to know how to retrieve a selected value from a dynamically selection box. If I get the selected value then I will store it into another variable that is located in another php file. This variable will help me in a sql query in postgresql.
//First php file
<form name="boton_alta_soniador" action="dar_alta_soniador.php" method="POST" autocomplete="off">
<div class="text-field">
Nombre de la asociacion
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxx password=xxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);
while($results [] =pg_fetch_object($result));
array_pop($results);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php foreach ( $results as $option ) : ?>
<option value="<?php echo $option->i_clave_asociacion; ?>"><?php echo $option->n_asociacion; ?></option>
<?php endforeach; ?>
</select>
</div>
</form>
This is just the dynamically selection box. Then I want to store the selected value in this variable:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
So I can query the following statement:
$conocerIDasociacion = "SELECT N_ASOCIACION FROM ASOCIACION WHERE I_CLAVE_ASOCIACION='$ingresaAsociacion'";
I didn't want to use jQuery because the whole system is almost entirely made in PHP and HTML.
Please, any help is welcome and I'm all ears to everyone.
Cheers!
Looks like you're missing a Submit button.
You do not have to use AJAX at all, nor jQuery. You can submit your form and process the selection value as you see fit.
The selected value in the select Tag will be sent to dar_alta_soniador.php, and that file will process that data, using the exact code you wrote:
$_POST['asociacion_seleccion']
So, in dar_alta_soniador.php you will write that code:
$ingresaAsociacion = pg_escape_string($_POST['asociacion_seleccion']);
And then perform the query. You do not even have to worry about sending the data around in a session variable, POST does it for you already.
So everything should be OK in your code, or I may have misunderstood your question. Do you have an error message or get some inappropriate behavior?
Maybe the submit button is missing? I use a code like this:
For the select tag:
<div class="controls">
<select name="list_name">
<option>List</option>
<?php
foreach ($nucleos as $inner_array) {
$out = "<option>" . $inner_array['name'] . "</option>";
echo $out;
}
?>
</select>
</div>
And for the Submit button:
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Confirm</button>
<br/>
</div>
</div>
</form>
I am using bootstrap here for style, HTML and CSS. Nothing more.
Best wishes,
I found another solution:
<? $strconn="dbname=postgres port=5432 host=127.0.0.1 user=xxxx password=xxxx";
$conn=pg_Connect($strconn);
$consulta="Select n_asociacion from asociacion";
$result=pg_query($conn,$consulta);?>
<select name="asociacion_seleccion" id="i_clave_asociacion">
<?php
while($row=pg_fetch_assoc($result))
{echo "<option>{$row['n_asociacion']}</option>";}?>
</select>
The point is, whenever the user selects an option from the dyamically selection box, the value of the selection box is known if we call the value with pg_escape_string($_POST['NAME OF YOUR SELECTION BOX']);.
Thanks to all for your collaboration,my problem was resolved.

echoing data from mysql_fetch_array

I'm trying to display data from my database table selected from a 'date'.
The query executes, but when I echo I don't get any result. Could you please help me with this?
<?php include 'includes/connection.php'; ?>
<html>
<head>
<title> </title>
</head>
<body>
<?php
if(isset($_POST['submitted'])){
$sql = "SELECT * FROM dagtaken WHERE datum = $_POST[datum]";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result)){
echo $row['aantal'];
}
}else{
?>
<form action='' method='POST'>
<p><input type="date" name="datum"></p>
<p><input type='submit' value='Dagtaak toevoegen' />
<input type='hidden' value='1' name='submitted' /></p>
</form>
<?php } ?>
</body>
</html>
The query shouldn't execute, since dates are very obviously strings and require quotes. That said...
Try this:
mysql_query("SLEECT * FROM `dagtaken` WHERE `datum`='".mysql_real_escape_string($_POST['datum'])."'");
Now on to the actual problem, you are checking if(isset($_POST['submitted'])), but nowhere do I see <input name="submitted" in your source (EDIT Never mind, it has a stupid amount of whitespace pushing it off the edge). Try if(isset($_POST['datum'])), since that's the variable you actually use.
You haven't named your submit button, so your PHP code never kicks in. Don't check for form fields when all you really need is to check if a POST has occured.
Quick fix for you code:
<input type="submit" name="submitted" value="Dagtaak toevoegen" />
^^^^^^^^^^^^^^^^^
Better fix:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
your code here ...
}
First, Escape your data. SQL injection is now very easy
Second, do you have data in your database?
Try print_r($row) instead of echo $row...
$_POST is with quotes...=> $_POST["datum"]
Last addition, is your date the same as your input?

Categories