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?
Related
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.
I'm beginner at PHP and I'm using a tutorial I found on the web to work a new piece of a new CMS I'm building. The problem is that the tutorial works just fine but when I start to alter it to my use it cannot connect to the database to update. I receive my error "Database Error: Unable to update record." I can add and delete but not update.
Index.php
<html>
<head>
<title>MySQLi Tutorial</title>
</head>
<body>
<?php
//include database connection
include 'db_connect.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM family WHERE fid = ".$mysqli->real_escape_string($_GET['fid'])."";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}
}
$query = "SELECT * FROM family";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results ){
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th>name</th>";
echo "<th>Action</th>";
echo "</tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>";
echo "<a href='edit.php?fid={$fid}'>Edit</a>";
echo " / ";
echo "<a href='#' onclick='delete_user( {$fid} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
//if table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
<script type='text/javascript'>
function delete_user( fid ){
//this script helps us to
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and fid to the record to be deleted
window.location = 'index.php?action=delete&fid=' + fid;
}
}
</script>
</body>
</html>
add.php
<html>
<head>
<title>MySQLi Create Record</title>
</head>
<body>
<!--we have our html form here where user information will be entered-->
<form action='#' method='post' border='0'>
<table>
<tr>
<td>Firstname</td>
<td><input type='text' name='name' /></td>
</tr>
<td></td>
<td>
<input type='hidden' name='action' value='create' />
<input type='submit' value='Save' />
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include 'db_connect.php';
//write query
$query = "insert into family
set
name = '" . $mysqli->real_escape_string($_POST['name']) ."'
";
if( $mysqli->query($query) ) {
echo "User was created.";
}else{
echo "Database Error: Unable to create record.";
}
$mysqli->close();
}
?>
</body>
</html>
edit.php
<?php
//include database connection
include 'db_connect.php';
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){
//write query
$query = "update users
set
name = '".$mysqli->real_escape_string($_POST['name'])."',
where
fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'";
if( $mysqli->query($query) ) {
echo "User was updated.";
}else{
echo "Database Error: Unable to update record.";
}
}
$query = "select fid, name
from family
where fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$fid = $row['fid'];
$name = $row['name'];
?>
<!--we have our html form here where new user information will be entered-->
<form action='#' method='post' border='0'>
<table>
<tr>
<td>Firstname</td>
<td><input type='text' name='name' value='<?php echo $name; ?>' /></td>
</tr>
<tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='fid' value='<?php echo $fid ?>' />
<!-- we will set the action to edit -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Edit' />
<a href='index.php'>Back to index</a>
</td>
</tr>
</table>
</form>
>
I have been unable to find the proper language to fix this but I believe it's because I"m using some form of multiples but I am not familiar with the mysqli/php languages to fix it.
Thank you
You need to remove the comma at the end of:
name = '".$mysqli->real_escape_string($_POST['name'])."',
the query should be:
$query = "update users
set
name = '".$mysqli->real_escape_string($_POST['name'])."'
where
fid='".$mysqli->real_escape_string($_REQUEST['fid'])."'";
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>
<iframe id="frame1" name="frame1" align="center" src="committee_assign1.php" height="400" width="700">
</iframe>
<center><input onClick="submitiframeform(); return false;" type="button" name="submit" value="Submit" />
<script type="text/javascript">
function submitiframeform(){
window.frames['frame1'].document.forms['fypassign'].submit();
}
</script>
The above is the main page name committee_assign.php ..
And below is the page where the iframe called committee_assign1.php.
<?php
include '../database.php';
include 'valid_login.php';
if(isset($_POST['submit'])) {
$continue = FALSE;
$i = 0;
while ($continue == FALSE) {
if (isset($_POST['id_'.$i])) {
$fypcomm = $_POST['fypcomm_'.$i];
$user = $_POST['id_'.$i];
$sql = mysql_query(" UPDATE Lecturer SET LectFypCommittee = '$fypcomm' WHERE LectID = '$user' ")
or die(mysql_error());
mysql_query($sql);
} else
{$continue = TRUE;}
$i++;
}
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='../committee/committee_assign1.php'
</SCRIPT>");
}
?>
<head>
</head>
<body>
<form id="fypassign" name="fypassign" method="post" action="" target="_self" onSubmit="">
<?php
$counter = 0;
echo "<table class ='box'>";
echo "<thead>";
echo "<tr>";
echo "<th align='left' valign='top'>"."Lecturer Name"."</th>";
echo "<th align='left' valign='top'>"."FYP Committee"."</th>";
echo "</tr>";
$sql = mysql_query(" SELECT * FROM Lecturer ORDER BY LectFypCommittee DESC, LectName ASC ") or die(mysql_error());
while($info = mysql_fetch_assoc($sql)) {
$idcount = "id_".$counter;
echo "<input type='hidden' name='$idcount' id='$idcount' value={$info['LectID']} />";
echo "<tr>";
echo "<td>";
echo $info['LectName'];
echo "</td>";
echo "<td>";
$formname = "fypcomm_".$counter;
echo "<select name='$formname'>";
//to convert the flag value to user understandable language
if ($info['LectFypCommittee'] == '0'){
$dbfyp = 'No';
}
else $dbfyp = 'Yes';
echo "<option selected='selected' value='{$info['LectFypCommittee']}'>".$dbfyp."</option>";
if ($info['LectFypCommittee'] == '0'){
echo "<option value='1'>".'Yes'."</option>";
}
else echo "<option value='0'>".'No'."</option>";
echo "</select>";
echo "</td>";
echo"</tr>";
$counter++;
}
echo "</table>";
?>
</form>
</body>
I clicked submit button at the parent page and the page refresh but the value is not update.
Can anyone here guide me on this please?
So sorry to post such long codes as I hope you guys could understand more what I am doing. TQ
You have no input in your committee_assign.php named submit:
if(isset($_POST['submit']))
You must check with something like this:
if(isset($_POST))
You are checking if submit has been posted but you dont have an input named submit. Add an input named submit with some value and check if that post submit exists. You can make it hidden if you dont want to see an extra input.
if($_POST['submit']) is checking if there is a value with key 'submit' in the post array. All the key and value in $_POST array is name and value of a form element respectively.
I have multiple forms on a single page and all gets redirected to same page when form is submitted but the previously submitted form values disappears when new form is submitted.
I tried using sessions didn't worked for me what else? please help
<script language="javascript">
function row(x,y){
//var len=document.forms[x].name.value;
if(x.value.length >=3)
{
//alert("Message form no> "+y+"will be submited");
document.forms[y].submit();
}
}
</script>
</head>
<body >
<center>
<h2>Database App</h2>
<table>
<tr>
<th><lable>Name :</label></th>
<th><label>E_Id :</label></th>
<th><label>Email :</label></th>
<th><label>Other Info :</label></th></tr>
<tr>
<?php
error_reporting(E_ALL ^ E_NOTICE);
// code check for name in database and if exists,displays in table row
//for($i=0;$i<150;$i++)
//{
//$E_id=array();
if($_POST){
$i = $_GET["uid"];
//echo "fhwefwej==".$i;
$x='name'.$i;
// echo 'dasvds'.$x;
if($_POST[$x])
{
$name = strtolower($_POST[$x]);
$E_id[$i] = "";
$Email[$i] = "";
$Otherinfo[$i] = "";
$con = mysql_connect('localhost','root','') or die("npt");
$db = mysql_select_db("trainee")or die("nptdff");
$query = "Select * from reguser where fname like '".$_POST[$x]."%'";
$result = mysql_query($query);
mysql_num_rows($result);
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$str=$row['fname'];
$initials = strtolower(substr($str,0,3));
if($name == $initials)
{
//echo "exist"."<br>";
$E_id[$i]= $row['fname'];
$Email[$i]=$row['lastname'];
$Otherinfo[$i]=$row['address'];
break;
}
}
}
else
{
$msg[$i] = "no user with these initials";
}
mysql_close($con);
}
}
for($i=0;$i<150;$i++)
{
//session_start();
//echo session_name($i)."<br>";
echo "<form name='form$i' action='new2.php?uid=$i' method='post'>";
echo "<td><input type='text' name='name$i' id='name$i' onkeyup='row(this,$i);' /><br />";
echo "<span id='availability_status' >";
if($_POST[$x]){echo $msg[$i];}
echo "</span> </td>";
echo "<td><input type='text' name='E_id' id='E_id' value='";
if(isset($_POST[$x])){ echo $E_id[$i];}
echo "' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='email' id='email' value='$Email[$i]' disabled='disabled' />";
echo "</td>";
echo "<td><input type='text' name='otherinfo' id='otherinfo' value='$Otherinfo[$i]' disabled='disabled' />";
echo "</td></tr>";
echo " </form>";
}
//echo '<script language="javascript">document.getElementById(\'name0\').focus();</script>';
?>
</table>
</center>
</body>
</html>
Why don't you Use AJAX. It will help to keep you posed different form information in back-end
you can either store those information in database or in file.
It will be the best way.