I have a dropdown box which is populated through MySQL:
echo "<form>";<br>
echo "Please Select Your Event<br />";
echo "<select>";
$results = mysql_query($query)
or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
echo "<option>";
echo $row['eventname'];
echo "</option>";
}
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";
How do i make it that if one clicks submit it will display a value from a MySQL db
Thanks for the help
Just change your query like SELECT result FROM somedb WHERE eventname = '".$eventname."'
Then you just do: (remember to check before while has user already requested info)
The value was: <?php print $row["result"]; ?>
Remember to check $_POST["eventname"] with htmlspecialchars before inserting it to query.
1) Give a name to your <select>, i.e. <select name='event'>.
2) Redirect your form to the display page (and set method POST): <form method='POST' action='display.php'>
3) just display the selected value: <?php echo $_POST['event']; ?>
If you want to use the same page, give a name to your submit button and then do this:
<?php
if (isset($_POST['submit']))
echo $_POST['event'];
?>
Hope it helps.
Related
I am currently building a web application. In my application, a load some data from mysql and I display them as a table in my website. Additionally I add another column that consists of different checkboxes. My source code of displaying the table is called by a function that is located in another page. The source code odf the function is the following :
function user_clients_table() {
$con = mysql_connect("localhost","root",'');
if(!$con){
die("Cannot Connect" . mysql_error());
}
mysql_select_db("client_app",$con);
$get_user_clients = "SELECT `ID`,`Name`,`SurName` FROM `clients` ";
$clients = mysql_query($get_user_clients,$con);
echo "<table border=2>
<tr>
<th>ID</th>
<th>Name</th>
<th>SurName</th>
<th>Receive Message</th>
</tr>";
while($record = mysql_fetch_array($clients)){
echo "<form action=pushnotification.php method=post>";
echo "<tr>";
echo "<td>".$record['ID']." </td>";
echo "<td>".$record['Name']." </td>";
echo "<td>".$record['SurName']." </td>";
echo "<td>"."<input type=checkbox name=checkbox[] value=".$record['ID']." />". "</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close();
}
The function works fine, after i call the function my webpage looks like this:
I want next to display the client number whose check box has been checked after i click the button send. For example if i checked only the first check box and submit it, i want to echo the client id that matches thsi checkbox, in this case i will echo '2'. My approach to this is the following:
if(isset($_POST['send'])){
if(!empty($_POST['checkbox'])) {
// Counting number of checked checkboxes.
$checked_count = count($_POST['checkbox']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['checkbox'] as $selected) {
echo "<p>".$selected ."</p>";
}
echo "<br/><b>Note :</b> <span>Similarily, You Can Also Perform CRUD Operations using These Selected Values.</span>";
}
else{
echo "<b>Please Select Atleast One Option.</b>";
}
}
It works but only for the first checkbox, if I select the other checkboxes without the first one I doesn't display anything.
Can someone please help me?
Thanks in Regards
Each checkbox is in it's own form, so when you are submitting you are submitting the first form (the first checkbox), and that is why you are getting the current action. Put the form tags outside the loop
I have table address in my database. I put the value=$Id; for my radio button but I have no idea why it's not working. No error shown make me even confuse and I need help since I am still new to php. When I put 2 address and I checked the radio button the address did not change. It only take the latest one that user inserted.
$userid=$_SESSION["userID"];
$sql="SELECT * FROM customer where ID='$userid'";
$result = mysqli_query( $con,$sql);
while($row = mysqli_fetch_array($result))
{
$Id=$row['Id'];
$OrderID=$row['OrderID'];
$ID=$row['ID'];
$Name=$row['Name'];
$Email=$row['Email'];
$Address=$row['Address'];
$PostalCode=$row['PostalCode'];
$City=$row['City'];
$State=$row['State'];
$mobile=$row['mobile_number'];
// $total=$total+$pSubtotal;
// $totalWeight=$totalWeight+$subWeight;
echo "<input type=\"radio\" name=\"select\" value=\"$Id\"> <label>$Name</label><br>";
echo "$Address<br>";
echo "$PostalCode $City<br>";
echo "$State";
echo "<div> </div>";
echo "<img src=\"images/phone-icon.png\" width=\"20\" height=\"20\"> $mobile<br>";
echo "<img src=\"images/email.png\" width=\"20\" height=\"20\"> $Email<br>";
echo "<div> </div>";
echo "<div class=\"col-xs-12 col-lg-12\" style=\"border-bottom:1px solid #ccc;\"></div>";
}
if(isset($_POST["pay"])){
$userid=$_SESSION["userID"];
$final_price=$_POST["final_price"];
$name=$_POST["name"];
$email=$_POST["email"];
$postcode=$_POST["postcode"];
//echo $postcode;
$mobile_number=$_POST["mobile_number"];
//echo $name;
$radio=$_POST['select'];
if(isset($radio)){
$sql="select Id,Name,mobile_number,Email,Address,PostalCode,City,State from customer where Id='$radio'";
echo $sql;
$res = mysqli_query($con,$sql);
if($rows=mysqli_fetch_array($res)){
$idx=$rows['Id'];
$name=$rows['Name'];
$mobile=$rows['mobile_number'];
$email=$rows['Email'];
$address=$rows['Address'];
$postcode=$rows['PostalCode'];
$city=$rows['City'];
$state=$rows['State'];
}
}
}
How are you submiting those information?
You just select the desired radio and press Submit?
Another point is, you do have 2 lines doing the same thing:
$Id=$row['Id'];
$ID=$row['ID'];
You should avoid those kind of thing.
Use the correct uppercase/lowercase according to your database table field name.
Did you use a var_dump($row) to check the correct case ?
That can be the problem.
Sorry if this is not the right answer, I'm not exactly sure what you are asking but I think you mean to use an associative array? Try using mysqli_fetch_assoc.
I am trying to fetch all "requestors" from a table and offer them as options in a form. As far as I know I didn't fetch the first row before but somehow my code misses the first entry.
Also when I submit the form and try to get the selected value with $_POST it doesn't take the selected value but instead takes the entry before that one.
Here is the code:
<select name="requestor">
<option value="%">Please select Requestor</option>
<?php
$con=mysqli_connect("localhost","username","password","database");
//============== check connection
if(mysqli_errno($con))
{
echo "Can't Connect to mySQL:".mysqli_connect_error();
}
$logins = mysqli_query($con, "select Login_name from login");
while ($dsatz = mysqli_fetch_assoc($logins))
{
echo $dsatz["Login_name"] . "<option value=". $dsatz["Login_name"]."</option>" . "<br />";
}
mysqli_close($con);
?>
</select>
try
echo "<option value='".$dsatz["Login_name"]."'>".$dsatz["Login_name"].'"</option>";
within the loop
the form already has an existing submit button which you can select difficulty(1st combo) and location(2nd combo) and then i submit this to display a video and a text field
under the field i have an addtional drop down box which is yes or no
then inside this code i want to submit a separate value using an if inisde an if it works fine on the 1st difficulty and location in the lists but nothing else and i cant see what im doing wrong a new pair of eyes would be great PS i will be upgrading this to PDO eventually
<?php
if(strpos($drop, 'norm') !== false && $ruavalue == 0)
{
Echo "RUA: ";
$RUAresult = mysql_query("SELECT Answer FROM options");
echo "<select name='ruacombo'>";
while($RUArow = mysql_fetch_assoc($RUAresult))
echo "<option value = '".$RUArow['Answer']."'>".$RUArow['Answer']."</option>";
echo "</select>";
echo '<form action="" method="post">';
echo "<input name=\"boss\" type=hidden value=".$_POST['tier_two'].">";
echo "<input name=\"main\" type=hidden value=".$_COOKIE['ID_my_site'].">";
echo '<input type="submit" name="ruasubmit" value="RUA!" />';
echo '</form>';
if (isset($_POST['ruasubmit']))
{
$ruaboss = $_POST['boss'];
$ruauser = $_POST['main'];
$ruasql = "UPDATE `RUASEXCELL` SET `$ruaboss`=1 WHERE Username = '$ruauser'";
$add_rua = mysql_query($ruasql);
}
In your while loop, you need to access the associative array keys as a string like so:
while($RUArow = mysql_fetch_assoc($RUAresult))
echo "<option value = '".$RUArow['Answer']."'>".$RUArow['Answer']."</option>";
Also, please look into using mysql_real_escape_string() to prevent SQL injection. In its current state, this script, and your database can be blown to pieces by a stray quote.
<?php
$i=1;
$ph='php';
echo "<form method=\"post\" action=\"\">";
$query=mysql_query("select question,id from ques where category=\"$ph\" order by rand() limit 5",$con);
if(!$query)
{
echo mysql_error().'query failed';
}
$ans=1;
while($value1=mysql_fetch_array($query))
{
echo "Q-$i"." ";
echo $value1['question']."<br />";
$i++;
$qno=$value1['id'];
$query1=mysql_query("select id,option1,option2,option3,option4 from ques where id=$qno");
if(!$query1)
{
echo mysql_error().'query failed';
}
while($value2=mysql_fetch_array($query1))
{
$opt=$value2['option1'];
$opt1=$value2['option2'];
$opt2=$value2['option3'];
$opt3=$value2['option4'];
$id=$value2['id'];
echo "<input type=\"radio\" value=\"$opt\" name=\"$ans\">";
echo "<span class=\"margin\">$opt</h1>";
echo "<input type=\"radio\" value=\"$opt1\" name=\"$ans\">";
echo "<span class=\"margin1\">$opt1</h2>";
echo "<input type=\"radio\" value=\"$opt2\" name=\"$ans\">";
echo "<span class=\"margin2\">$opt2</h3>";
echo "<input type=\"radio\" value=\"$opt3\" name=\"$ans\">";
echo "<span class=\"margin3\">$opt3</h4>"."<br /><br />";
$ans++;
}
}
echo"<center>"."<input type=\"submit\" name=\"submit\" value=\"submit\">"."</center>";
echo "</form>";
if(isset($_POST['submit']))
{
$correct=$_POST[1];
$_SESSION['value']=$correct;
/*$correct1=$_SESSION['value'];
echo $correct1;*/
$correct2=$_POST[2];
$_SESSION['value1']=$correct2;
/*$correct3=$_SESSION['value1'];
echo $correct3;*/
$correct4=$_POST[3];
$_SESSION['value2']=$correct4;
/*$correct5=$_SESSION['value2'];
echo $correct5;*/
$correct6=$_POST[4];
$_SESSION['value3']=$correct6;
/*$correct7=$_SESSION['value3'];
echo $correct7;*/
$correct8=$_POST[5];
$_SESSION['value4']=$correct8;
/*$correct9=$_SESSION['value4'];
echo $correct9;
$j=1;
while($j<=$i-1)
{
$correct=$_POST[$j];
$_SESSION['$j']=$correct;
$j++;
$correct1=$_SESSION['$j'];
echo $correct1."<br />";
}*/
header("location:quizresult.php");
}
?>
I don't know how to send id of selected option to the next page.I want to retrieve the id of selected option in the next page because from this i can get the correct answer of the selected question or any other solution of this and i get the only one id on the next page
There are several ways how to carry over values to the next page. I used the following successfully before:
1. Use a $_SESSION variable
Save the option values in session variables and retrieve them on the next page.
2. Use hidden form fields
You can save them as part of the form but in a hidden form field and retrieve it on the next page via $_POST.
An excellent tutorial (4 pages) for HTML forms and its various inputs is here:
http://www.javascript-coder.com/html-form/html-form-tutorial-p1.phtml
3. Use a http query
You can append a http query to your link to the next page, containing the values, i.e.
http://www.example.com/next-page?option1=value1&option2=value2
On the next page you can use parse_url($url, PHP_URL_QUERY) to extract the values. I would recommend that only if you do not submit a form though. I use this option if I create action buttons that are linking to the next (or the same) page and a single action has to be performed before the page is displayed.
Some generic info about query strings: http://en.wikipedia.org/wiki/Query_string
$qno= $_SESSION['id'] = $value1['id'];
Id is being saved in $_SESSION['id'] and will be available on the ext page also.
retrieve it like this
$id = $_SESSION['id'];