I want to get the selected radio button's value in php.
code:
update.php
<html>
<head>
<link rel="stylesheet" href="css/common.css" type="text/css">
<link rel="stylesheet" type="text/css" href="css/page.css">
<link href="css/loginmodule.css" rel="stylesheet" type="text/css" />
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "gettheater.php?q=" + str, true);
xmlhttp.send();
}
</script>
</head>
<body>
<h1>Welcome <?php echo $_SESSION['SESS_FIRST_NAME']; ?></h1>
My Profile | Logout
<p>This is a password protected area only accessible to Admins. </p>
<center>
<div class="pan"><br><br>
<div class="head">Remove Theater From List</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<table>
<tr>
<td>
<table>
<tr>
<td><label for="Theatername">Theater Name</label></td>
<td>
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost', 'tiger', 'tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT theater_name FROM theater;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
echo "<option>----Select Theater----</option>";
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['theater_name'] . "'>" . $row['theater_name'] . "</option>";
}
echo "</select>";
?>
</td>
</tr>
</table>
</td>
</tr>
</form>
<tr>
<td>
<form method="POST">
<table>
<tr>
<td><label for="Address">Address</label></td>
<td><div id="txtHint">
</div></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name='Remove From List' value="Remove From List" class="getvalue" onclick="< ? php myfunc() ? >"/></td>
</tr>
</table>
</table>
</form>
<br><br>
</tr>
</td>
<?php
if (isset($_POST['Submit'])) {
echo $_POST['address'];
}
?>
</div>
</center>
</body>
</html>
Code
gettheater.php
<?php
$q = strtolower(trim($_GET["q"]));
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost', 'tiger', 'tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'SELECT address FROM theater WHERE LOWER(theater_name) = :q';
$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
$sth->execute();
echo "<form name='theater' method='POST' action='update.php'>";
echo "<table border='0' class='tabs'><tr><th>Theater Address</th><th>Select</th></tr>";
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td class='ad'>" . $row['address'] . "</td>";
echo "<td>";
echo '<input type="radio" name="address" value="43" />';
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
$dbh = null;
?>
The html for the radio button is in gettheater.php page. but I am running the update.php
the gettheater page is opening in update.php but how can I get the selected radio button value and I want to get the selected radio button value in update.php.
How can I do this?
Check if the form has been submitted, if it has then use $_POST[] to access the data.
See http://php.net/manual/en/reserved.variables.post.php
You may want to include a submit button, this will allow you to check if the form has been submitted in the first place.
echo "<form name='theater' method='POST' action='update.php'>";
echo '<input type="submit" value="Enter" name="submit">';
To check
if (isset($_POST['submit'])) {
//Do something
}
This will usually work, if it doesn't then try using a hidden field.
Related
I'm new in PHP. some one please help to stop inserting blank data when refreshing the browser. I'm trying to insert data in SQL database from a HTML form & showing them in the same page in a table.
here is my from & PHP code
Thanks in advance....
</head>
<body>
<?php
$sub = mysql_connect ("localhost", "root","");
if (!$sub)
{
die('Could Not Connect:'.mysql_erro());
}
mysql_select_db("test", $sub);
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
echo 'One Record added';
$result=mysql_query("SELECT * FROM te");
echo "<table border='1'>;
<tr>
<th>Date</th>
<th>Sadi</th>
<th>Jam</th>
<th>Washi</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['Dat']."</td>";
echo "<td>" .$row['Sadi']."</td>";
echo "<td>" .$row['Jam']."</td>";
echo "<td>" .$row['Washi']."</td>";
echo "</tr>";
}
echo "</table>";
Mysql_close($sub);
?>
</body>
<form action="index.php" method="post">
<input type="text" name="Dat" placeholder="Date, DD-MM-YY">
<input type="text" name="Sadi" placeholder="Sadi">
<input type="text" name="Jam" placeholder="Jam">
<input type="text" name="Washi" placeholder="Washi">
<input type="submit" name="sub">
</form>
Just verify if the $_POST is not empty
if(!empty($_POST))
{
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
}
I have a simple search script like this and works fine:
<html>
<head>
<title>any</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script>
function check() {
var searchtext = document.getElementById("txt").value;
if(searchtext == '') {
alert('Enter string to search');
} else {
document.myform.submit();
}
}
function reset_table() {
document.myform.submit();
}
</script>
</head>
<body>
<?php
error_reporting(0);
require_once "config.php";
$link = mysql_connect($hostname, $username, $password);
$dbcon = mysql_select_db($dbname);
echo "<div align='center' class='resp_code'>";
echo "<h5><b>...</b></h5>";
echo "<div align='center'>";
echo "<form method='post' action='index.php' id='searchform' class='frms' name='myform'>
<input type='text' id='txt' name='name' autocomplete='off'>
<input type='submit' name='submit' value='Search' id='search' onclick='check();'>
<input type='submit' name='reset' value='Reset' id='reset' onclick='reset_table()'>
</form> ";
echo "</div>";
echo "<div align='center'>";
$search = $_POST["name"];
if(isset($_POST['name'])) {
echo "<table class='table'>
<tr>
<th>No.</th>
<th>Un.</th>
<th>img</th>
</tr>";
$qry = mysql_query('SELECT * FROM rs_posts WHERE id like "%'.$search.'%"');
$count = mysql_numrows($qry);
if($count > 0) {
if($qry) {
while($row = mysql_fetch_row($qry)) {
$num = $row['id'];
$uniq = $row['unique_id'];
$img = $row['featured_img'];
echo "<tr>
<td id='num'>$row[0]</td>
<td id='uniq'>$row[1]</td>
<td id='img'>$row[25]</td>
</tr>";
}
} else {
echo "No";
}
} else{
$op = "No Results Found";
}
}
echo "</div>";
echo "<div style='font-weight:bold;color:red;'>$op</div>";
echo "</div>";
?>
</body>
</html>
and have a link code like this:
link
I need some of search results like
<td id='img'>$row[25]</td>
link to that. What's the most simple way to do that?
If accept alert or click on RESET button it show all content of my db but I need hide them when I don't input anything or click RESET button. How can resolve I that?
So, whenever I click the add button and update the database, I have to refresh the page to see the results instead of the page auto refreshing after I click "update database".
Also, my delete button doesn't work at all. Please help D:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Movie Project</title>
</head>
<body>
<h1 style='text-align:center;'>Movie Collection Database</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method='post'>
<table align='center' border='0' cellpadding='3'>
<tr>
<?php
$fieldsArray = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run Time(min)", "Add");
echo "<tr>";
for($i=0;$i<7;$i++)
{
echo "<td>". $fieldsArray[$i]. "</td>";
}//end for
echo "</tr>";
?>
</tr>
<tr>
<td colspan='1' style='text-align:center'>
<input type="text" name="title" value="Movie Title">
</td>
<td>
<input type="text" name="studName" value="Studio Name">
</td>
<td>
<select name="movieRating">
<option value="G">G</option>
<option value="PG">PG</option>
<option value="PG-13">PG-13</option>
<option value="R">R</option>
<option value="NC-17">NC-17</option>
<option value="Not Rated">Not Rated</option>
</select>
</td>
<td>
<input type="text" name="pubYear" value="2015">
</td>
<td>
<input type="number" name="IMDBRating" value="10.0">
</td>
<td>
<input type="number" name="time" value="0">
</td>
<td>
<input type="checkbox" name="addBox">
</td>
</tr>
</table>
<table align='center'>
<tr>
<td>
<input type="submit" name="submit" value="Update Database">
</td>
</tr>
</table>
</form>
<?php
echo "<table align = 'center', border = '1' >";
try
{
$db = new PDO("mysql:host=localhost;dbname=buckel", "buckel", "12345");
}
catch (PDOException $error)
{
die("Connection failed: ". $error->getMessage());
}
try
{
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $error)
{
$db->rollback();
echo "Transaction not completed: " . $error->getMessage();
}
include_once("Function.php");
if($_SERVER['REQUEST_METHOD']==="POST")
{
Delete($db);
$fieldsArray = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run Time(min)", "Delete" );
echo "<tr>";
for($i=0;$i<7;$i++)
{
echo "<td>". $fieldsArray[$i]. "</td>";
}//end for
echo "</tr>";
$query = "SELECT * FROM movie";
$statement = $db->prepare($query);
$statement->execute();
foreach(Prepare($db) as $row)
{
echo "<tr><td>" .$row['title'] . "</td>";
echo "<td>" .$row['studio'] . "</td>";
echo "<td>" .$row['rating'] . "</td>";
echo "<td>" .$row['pub_year'] . "</td>";
echo "<td>" .$row['imdb_rating'] . "</td>";
echo "<td>" .$row['run_time'] . "</td>";
echo "<td><input type='checkbox' name='". $row['id']. "' value='". $row['id']. "'>Delete</td></tr>";
}//end foreach
echo "</table>";
if(isset($_POST['addBox']))
{
$title=$_POST['title'];
$studio=$_POST['studName'];
$year=$_POST['pubYear'];
$movieRating=$_POST['movieRating'];
$IMDBRating=$_POST['IMDBRating'];
$time=$_POST['time'];
try{
$query2= $db->prepare("INSERT INTO movie (id, title, studio, rating, pub_year, imdb_rating, run_time) value (".'NULL'.", '$title','$studio','$movieRating','$year','$IMDBRating','$time')");
$query2->execute();
}//end try
catch(PDOException $error)
{
$db->rollback();
echo "There was an error";
$db=null;
die("Connection failed:" . $error->getMessage());
}//end catch
}//end if
if(isset($db))
$db=null;
}//end if for whole thing
?>
</body>
</html>
and here is the function file that i'm calling
<?php
function Prepare($db)
{
$q = $db->prepare("SELECT * FROM movie");
$q->execute();
return $q->fetchAll();
}//end Prepare(db object)
function Delete($db)
{
$q = $db->prepare("SELECT * FROM movie");
$q->execute();
foreach($q->fetchAll() as $row)
{
if(array_key_exists($row['id'], $_POST))
{
$q = $db->prepare("DELETE FROM movie WHERE movie.id=" . $row['id']);
$q->execute();
}//end if
}//end foreach
}//end Delete(db object)
You are using self page for printing showing etc.
If wanna to refresh page without reloading you have to use
else you can try like this
Javascript
window.location.reload(true);
// false - Default. Reloads the current page from the cache.
// true - Reloads the current page from the server
This will reload the page.
Php
header("Refresh:0");
I fixed the delete part not working by putting all of my php inside the form. I suppose that the php was not being submitted with the form
When a checkbox is ticked it should generate a time field in the next php file, however the page only generates 1 selected checkbox instead of all.
<html>
<head>
<style>
table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid gray;
}
table {
width: 80%;
}
th {
height: 30px;
}
</style>
<script type ="text/javascript">
function load() {
if (!document.getElementById('checkbox1').checked) {
return document.getElementById('show').innerHTML = '';
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('show').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'include.inc.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<p><?php include 'header.php'; ?></p>
<div align="justify">
<td><form method="post" action="search.php">
Name: <input type="text" name="search" />
<input type="submit" name="submit" value="Search">
</form></td>
<td><form method="post" action="grouprank.php">
Rank: <input type="text" name="groupby" />
<input type="submit" name="submit" value="Group by">
</form></td>
<?php
require ("dbfunction.php");
$con = getDbConnect();
?>
<td>
<td> <input type="checkbox" id ='checkbox1' name="checkbox1" onclick="load();" value="Bike">Include previous service terms</td> <!-- database -->
</div>
<form name="form" id="form" action="multiedit.php" method="post">
<div id="show">
</div>
<p><table>
<tr>
<th>Tick</th>
<th>Name</th>
<th>Rank</th>
<th>Start Date</th>
<th>End Date</th>
<th>Watchkeeping</th>
<th>Active</th>
</tr> <!-- database -->
<tr>
<?php
if (!mysqli_connect_errno($con)) {
$queryStr = "SELECT * " .
"FROM crewlist";
}
$result = mysqli_query($con, $queryStr);
while ($row = mysqli_fetch_array($result)) {
if (date("Y-m-d") > $row['start_date'] && date("Y-m-d") < $row['end_date']) {
echo "<tr><th>" . "<input type = \"checkbox\" id ='checkbox2' name = 'checkbox2' value=\"".$row['crew_id']."\" >" . "</th>";
echo "<th>" . "" . $row["crew_name"] . "";
echo "<th>" . $row["crew_rank"] . "</th>";
echo "<th>" . $row["start_date"] . "</th>";
echo "<th>" . $row["end_date"] . "</th>";
echo "<th>" . $row["watchkeeping"] . "</th>";
echo "<th>" . $row["active"] . "</th>";
} else {
}
}
?>
</tr>
<input type="submit" value="Submit" ></td>
</tr>
</table>
</form>
</body>
Here is the next page
<?php include 'header.php'; ?>
<div id="container4"><?php
require ("dbfunction.php");
$con = getDbConnect();
$checkbox2 = $_POST['checkbox2'];
if (!mysqli_connect_errno($con)) {
$queryStr = "SELECT * " .
"FROM crewlist WHERE crew_id =".$_POST['checkbox2'];
}
$result = mysqli_query($con, $queryStr);
print_r($_POST);
while ($row = mysqli_fetch_array($result)) {
if (date("Y-m-d") > $row['start_date'] && date("Y-m-d") < $row['end_date']) {
echo "<tr><th>" . $row["crew_name"] . ":</th><br>";
echo " <tr>
<td>Shift 1:</td>
<td><input type=\"time\" name=\"start_hour\" value=\"start_hour\" id=\"start_hour\" step=\"1800\" required> to <input type=\"time\" name=\"end_hour\" value=\"end_hour\" id=\"end_hour\" step=\"1800\" required>
</td>
</tr>
<tr>
<td>Shift 2:</td>
<td><input type=\"time\" name=\"start_hour2\" value=\"start_hour2\" id=\"start_hour2\" step=\"1800\" required> to <input type=\"time\" name=\"end_hour2\" value=\"end_hour2\" id=\"end_hour2\" step=\"1800\" required>
</td>
</tr><br><br>";
} else {
}
}
?>
I've tried running a foreach loop but there seems to be a syntax error with the brackets
first of all I'm using this site as a template for what I'm trying to do except im adding a search box on top of this: http://www.dougv.com/demo/jquery_sort_records/js.php (due to reference blocking you should copy and paste the link to view it. else you will run into a 403 forbidden error)
I want to create a simple search box that will search upon a certain attribute and display any results found (basically a filter). Here's what I have so far as part of the code.
so far the table appears well however I want to add a search box that will search upon the first name attribute.
any help?
<script type="text/javascript" src="jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="jquery.tablesorter.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#sortedtable").tablesorter({ sortlist: [0,0] });
});
</script>
</head>
<body>
<?php
if(!$link = mysql_connect("database_server", "user_name", "password")) {
echo "Cannot connect to db server";
}
elseif(!mysql_select_db("database_name")) {
echo "Cannot select database";
}
else {
if(!$rs = mysql_query("SELECT * FROM table")) {
echo "Cannot parse query";
}
elseif(mysql_num_rows($rs) == 0) {
echo "No records found";
}
else {
echo "<table id=\"sortedtable\" class=\"bordered\" cellspacing=\"0\">\n";
echo "<thead>\n<tr>";
echo "<th>Record ID</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Birthday</th>";
echo "<th>Department</th>";
echo "</tr>\n</thead>\n";
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>$row[person_id]</td><td>$row[person_name]</td><td>$row[person_surname]</td><td>$row[person_birthdate]</td><td>$row[person_department]</td></tr>\n";
}
echo "</table><br />\n";
}
}
?>
<form name="search_form" method="POST" action="js.php">
<font face="Calibri"> </font>
Search: <input type="text" name="search_b" value="" />
<input type="submit" name="search" value="Search for your term..."> </input>
</form>
First i would advise you to enclose array acces in strings like this: `" some tring {$array['key']} " thus you won't run into any problems accessing arrayfields directly in your string.
But that should do it now:
<script type="text/javascript" src="jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="jquery.tablesorter.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#sortedtable").tablesorter({ sortlist: [0,0] });
});
</script>
</head>
<body>
<?php
if (!$link = mysql_connect("database_server", "user_name", "password")) {
echo "Cannot connect to db server";
} elseif (!mysql_select_db("database_name")) {
echo "Cannot select database";
} else {
$searchString = "";
if(!empty($_POST['search_b'])){
$searchString = "WHERE user_name LIKE '%{$_POST['search_b']}%'";
}
if (!$rs = mysql_query("SELECT * FROM table $searchString")) {
echo "Cannot parse query";
} elseif (mysql_num_rows($rs) == 0) {
echo "No records found";
} else {
echo "<table id=\"sortedtable\" class=\"bordered\" cellspacing=\"0\">\n";
echo "<thead>\n<tr>";
echo "<th>Record ID</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Birthday</th>";
echo "<th>Department</th>";
echo "</tr>\n</thead>\n";
while ($row = mysql_fetch_array($rs)) {
echo "<tr><td>{$row[person_id]}</td><td>{$row[person_name]}</td><td>{$row[person_surname]}</td><td>{$row[person_birthdate]}</td><td>{$row[person_department]}</td></tr>\n";
}
echo "</table><br />\n";
}
}
?>
<form name="search_form" method="POST" action="js.php">
<font face="Calibri"> </font>
Search: <input type="text" name="search_b" value="" />
<input type="submit" name="search" value="Search for your term..."> </input>
</form>