I have created a drop-down list that shows the members which have been added from the user. The script for this is this one:
<?php include('server.php'); ?>
<?php
$connect = mysqli_connect("localhost", "root", "", "geofence");
?>
<html>
<head>
<title>Add members</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br /><br />
<h2 align="center">Please, choose a family member</h2>
<br /><br />
<div class="form-group" align="center">
<form name="dropdown" action="" method="post">
<select name="choose" id="choose" width="150" style="width: 150px">
<option value="" selected disabled hidden>Choose Member</option>
<?php
$res=mysqli_query($connect,"select *
from member
where user_id=".$_SESSION['user_id']."");
while($row=mysqli_fetch_array($res)) {
?>
<option><?php echo $row["name"]; ?></option>
<?php
}
?>
</select>
</form>
<input type="button" name="ok" id="ok" class="btn btn-info" value="OK" />
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#ok').click(function(){
$(location).attr('href', 'http://localhost/Houston/index.php')
});
});
</script>
Now, what I'm trying to do is to create a session ($_SESSION['member_id']) which refers to the name that is selected from the list. I have tried several things so far but with no success. An example is this:
if (isset($_POST['choose'])) {
if (count($errors)==0) {
$query="SELECT * FROM member where name=".$_POST['choose']."";
$result=mysqli_query($db,$query);
if (mysqli_num_rows($result)==1) {
$row=mysqli_fetch_assoc( $result);
$member_id=$row['member_id'];
$_SESSION['member_id'] = $row['member_id'];
}
}
}
I need the $_SESSION['member_id'] so I can use it on another file and populate my database. What is missing?
The problem is in the option tag, you have to specify de value of each option in the select tag , like this:
<option value="<?php echo $row["member_id"];?>"> <?php echo $row["name"]; ?> </option>
So, next you just have to declare the $_SESSION for your option value
if (isset($_POST['choose'])) {
$_SESSION['member_id']=$_POST["choose"];
}
Related
i have a problem with deleting the articles. I want it to delete after click on submit button but i dont know how. I do not want use javascript to autosubmit form in select tag. Can u help me ? I would appriciate that. Thanks.
<?php
session_start();
include_once('../includes/conn.php');
include_once('../includes/article.php');
$article= new Article;
if(isset($_SESSION['logged_in'])){
if(isset($_POST['id'])){
$id=$_POST['id'];
$query=$pdo->prepare('DELETE FROM articles WHERE article_id=?');
$query->bindValue(1, $id);
$query->execute();
header('Location: delete.php');
}
$articles=$article->fetch_all();
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
CMS
<br/>
<h4>Delete article:</h4>
<form action="delete.php" method="post" name="id">
<select>
<?php foreach($articles as $article){?>
<option value="<?php echo $article['article_id']; ?>"><?php echo $article['article_title']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Delete article">
</form>
</div>
</body>
?>
According to your code, shift name="id" from form to select.
You are trying to get selected id of drop down, so the name should be given to select.
<form action="delete.php" method="post" name="id">
<select>
to
<form action="delete.php" method="post">
<select name="id">
Another issue noticed in your code is, you should terminate execution
immediately after header('Location: delete.php'); with die(); or
exit(); to ensure remaining lines should not be executed. In PHP,
header() is just a function which helps in setting header, and here
you are settling Location header, which further handled by browser
for taking necessary action (here redirection). So, header() does
not ensure stopping execution of remaining code.
add <?php echo $_SERVER['PHP_SELF']; ?> to the form instead of delete.php to execute the php code above and i assume the file looks as your post .
and add name="id" to select element to grap the post value to manipulate instead of form.
i hope this works according to what you provide in your post.
session_start();
include_once('../includes/conn.php');
include_once('../includes/article.php');
$article= new Article;
if(isset($_SESSION['logged_in'])){
if(isset($_POST['id'])){
$id=$_POST['id'];
$query=$pdo->prepare('DELETE FROM articles WHERE article_id=?');
$query->bindValue(1, $id);
$query->execute();
header('Location: delete.php');
}
$articles=$article->fetch_all();
?>
<html>
<head>
<title>CMS Tutorial</title>
<link rel="stylesheet" href="assets/style.css" />
</head>
<body>
<div class="container">
CMS
<br/>
<h4>Delete article:</h4>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="id">
<?php foreach($articles as $article){?>
<option value="<?php echo $article['article_id']; ?>"><?php echo $article['article_title']; ?></option>
<?php } ?>
</select>
<input type="submit" value="Delete article">
</form>
</div>
</body>
I'm just learning how to do this so please forgive my ignorance!
Here is my test site: http://webtestkit.com/1KaraokeDJ/index.php
First, I found this code in a learning example and that code worked just fine. (http://www.mostlikers.com/2013/08/search-engine.html) - no problems. I even made the sample database and checked all was working.
now I wanted to change it to work for my purpose (a karaoke search)...
here is my code:
<?php
include("connect.php");
session_start();
if(isset($_POST['submit']))
{
$search=$_POST['search'];
$_SESSION['title']= $search;
if(($_SESSION['title'])!="")
{ header("location:index.php"); }
else
{ echo "<script> alert('Please enter something to search for') </script>"; }
}
?>
<html>
<head>
<title>1KaraokeDJ.com</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="login">
<form method="post">
<p><img src="top.jpg" /></p>
<p>
<?php if(isset($_SESSION['title'])) { ?>
<input name="search" type="search" list="searchkey" value="<?php echo $_SESSION['title'];?>" class="search" />
<?php } else { ?>
<input name="search" type="search" list="searchkey" placeholder="Just type your text here and press enter - ex : Abba" class="search" />
<?php } ?>
</p>
<datalist id="searchkey">
<?php
$tile=$db->query("SELECT * FROM `1KaraokeDJ.com`");
while($storetitle=mysqli_fetch_object($tile))
{ ?>
<option value="<?php echo $storetitle->title ?>">
<?php } ?>
</datalist>
<p><input type="submit" name="submit" id="click" class="searchbutton" value="Karaoke Search" /></p>
<?php if(isset($_SESSION['title'])) {
if(($_SESSION['title']!=""))
{
$data=$_SESSION['title'];
$view=$db->query("select * from 1KaraokeDJ.com where title like '%$data%' limit 10");
$check=mysqli_num_rows($view);
if($check!="")
{
while($descri=mysqli_fetch_object($view))
{
?>
<div class="reslt">
<h3 id="resuil-title"><?php echo $descri->title; ?></h3>
<p class="Description">
<?php $description = str_replace($data, '<span class="highlight">'.$data."</span>", $descri->artist);
echo $description; ?>
<p>
<hr>
</div>
<?php } } else { ?>
<div class="reslt">
<h3 id="resuil-title">Nothing fond!</h3>
<p class="Description">Try changing your search terms<p><hr>
</div>
<?php } } } ?>
</form>
</div>
</body>
</html>
The search field is finding data in the dropdown list so the connection is fine and the search works.
I think I understand most of the code, but I don't seem to understand this:
$view=$db->query("select * from 1KaraokeDJ.com where title like '%$data%' limit 10");
$check=mysqli_num_rows($view);
if($check!="")
{ while($descri=mysqli_fetch_object($view)) {
My If always goes to else "Nothing Found"
Any help? The way I learn is by doing - so it's trial and error until I figure it out!
Your table 1KaraokeDJ.com is interpreted as DatabaseName.TableName
To avoid this, escape your table name
Select * from `1KaraokeDJ.com` Where ...
this is my code
<select>
<?php
$query = "SELECT * FROM 'sections'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}
?>
</select>
nothing happen I don't know why
this my page maybe there is wrong some where
<?php
ob_start();
session_start();
include('../includes/connect.php');
include('../includes/phpCodes.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>لوحة التحكم</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../css/mainstyle.css">
<link rel="stylesheet" type="text/css" href="css/controlstyle.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#tabs div').hide();
$('#tabs div:first').show();
$('#tabs ul li:first').addClass('active');
$('#tabs ul li a').click(function(){
$('#tabs ul li').removeClass('active');
$(this).parent().addClass('active');
var currentTab = $(this).attr('href');
$('#tabs div').hide();
$(currentTab).show();
return false;
});
});
</script>
</head>
<body>
<div class="wrapper">
<?php headerCode(); ?>
<div class="content">
<div id="tabs">
<ul>
<li>اضافة موضوع</li>
<li>حذف موضوع</li>
<li>تعديل موضوع</li>
<li>التحكم بالاقسام</li>
</ul>
<div id="add">
<form method="POST" action="includes/add.php" dir="rtl" enctype="multipart/from-data">
<br>
حدد القسم :
<select>
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
}?>
</select><br>
عنوان الموضوع :<input type="text" name="title" class="mem-information"/><br>
الموضوع : <br /><textarea name="subject" rows="10" cols="50" class="mem-information" style="width: 500px"></textarea><br /><br>
الصورة :<input type="file" name="image"><br>
<input type="submit" value="إرسال" name="send" class="log" style="color: black">
</form>
</div>
<div id="remove">
<form method="POST" action="includes/remove.php" dir="rtl"><br>
حدد القسم :
<select name ="sectionsName">
<option value="">dd</option>
</select>
<input type="submit" value="حذف" name="send" class="log" style="color: black">
</form>
</div>
<div id="edit">
</div>
<div id="addDep">
</div>
</div>
</div>
</div>
</body>
</html>
sorry for my bad English
my table
The problem I see is you are using single quotes on your table name in your query, you should either use back tick or no back ticks for table name. Please try the following code:
<?php
$query = "SELECT * FROM `sections`";
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<option value='".$row['id']."'>".$row['sectionName']."</option>";
}
?>
Also start looking into using mysqli (http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), as mysql is deprecated.
Try this... And we are all Asuming the name of the column is "sectionsd" with the "d".
<?php
$query = "SELECT * FROM 'sectionsd'";
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)){
echo "<option value='".$row["id"]."'>".$row["sectionName"]."</option>";
}?>
</select>
If it's not working it may be due to:
1) You're using a mysql_ function and you're not sending the parameter $link to the function. Like:
mysql_query($query, $connectionlink);
2) The table name is wrong
3) You have an error: Use mysql_query() or die(mysql_error()); to see what's going on
4) DO NOT use single quotes ' around your table name in the query
echo '
<option value='.$row["id"].'>'.$row["sectionName"].'</option>
';
Can anyone please help me on this:
I am trying to add a classname/ change the css of div using jquery; after I click submit on the form. Means, after clicking submit and when the page reloads this classname/ changes should stick to the target element. I am using php to get the values of form submitted. (no ajax, but if necessary I can modify my code). I tried to do this by jquery but when the page reloads the changes are gone !
Thanks.
update: Here is the sample code(so.php itself) I am working on. after form is submitted and page is reloaded; say I want to add a class to input that is clicked and manage the style in CSS:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<div id="container">
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
update2: I tried the logic provided by #NoPyGod and it worked. But here is the thing. I am trying to add a class to the button that is selected as well as update the text in the div with id "hello" with the value of input that is clicked or in active state once the form is submitted and page is reloaded. This is the one I ended up but once form is submitted
all the inputs are having the same class name as well the text in div is also not updated.
<html>
<head>
<?php if (isset($_GET['Load'])): ?>
<script>
$(document).ready(function() {
$('#hello').text('Hello');
});
</script>
<?php endif; ?>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Google" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Rediff" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load" type="submit" value="Yahoo" />
<input class="<?php if (isset($_GET['Load'])) { echo "some_class"; } if (isset($_GET['Load1'])) { echo "some_class1"; }?>" name="Load1" type="submit" value="Sify" />
</form>
<div id="container" >
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
if (isset($_GET['Load1'])) {
$value = $_GET['Load1'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
<div id="hello"></div>
</body>
</html>
When the page is submitted, set a variable like so
$was_submitted = true;
Then in your HTML do something like this
<div id="myid" class="<?php if ($was_submitted) { echo "some_class"; } ?>">
Or if you absolutely insist on using jQuery to change it, include this in your html --
<?php if ($was_submitted): ?>
<script>
$(document).ready(function() {
$("#myid").addClass("some_class");
});
</script>
<?php endif; ?>
Updated:
<html>
<head>
</head>
<body>
<form id="myForm" action="so.php" method="get">
<input name="Load" type="submit" value="Google" />
<input name="Load" type="submit" value="MSN" />
<input name="Load" type="submit" value="Yahoo" />
</form>
<!-- I changed this line below -->
<div id="container" class=<?php if (isset($_GET['Load'])) { echo "some_class"; } ?>>
<?php
if (isset($_GET['Load'])) {
$value = $_GET['Load'];
echo "<iframe width=\"560\" height=\"349\" src=\"http://www.{$value}.com\"></iframe>";
}
?>
</div>
</body>
</html>
I have a drop down box that contains a list of names and a search field.
The drop down is populated with a list of names from the database and the search field allows you to perform a wild card search.
At the moment the wild card search works as expected but choosing a name from the drop down does not work.
I believe this might be possibly because of some unwanted characters as I am seeing the below in my address bar on the browser having chosen a name from the drop down list and clicked the search button:
http://localhost:81/connect/players/?name=%0D%0A3&text=&action=search
I think that text above (%0D%0A) is causing a problem as my code looks like this:
if (isset($_GET['action']) and $_GET['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
$id = $_GET['name']; // name slightly confusing but does return the id
$text = $_GET['text'];
try
{
$sql = "SELECT id, name, age FROM player
WHERE player.id = '$id'
OR player.name LIKE '%$text%'
GROUP BY player.id";
$s = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching names.' . $e->getMessage();;
include 'error.html.php';
exit();
}
// This is responsible for populating the new player info underneath all
foreach ($s as $row)
{
$names[] = array('id' => $row['id'], 'name' => $row['name'], 'age' => $row['age']);
}
include 'searchprofiles.html.php';
exit();
}
And I believe this is preventing it from comparing the id in the database with the id that is stored in the variable $id.
I have however also just manually stripped %0D%0A out from the address bar and it still doesn't work so perhaps there might be another issue?
It should also be noted that if no value is selected from the drop down and no wild card is entered then all rows are returned.
HTML is as follows:
SEARCHPROFILES.HTML.PHP
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Jokes: Search Results</title>
</head>
<body>
<h1>Search Results</h1>
<?php if (isset($names)): ?>
<table>
<tr><th>Name</th><th>Options</th></tr>
<?php foreach ($names as $name): ?>
<tr>
<td><?php htmlout($name['name']); ?></td>
<td><?php htmlout($name['age']); ?></td>
<td>
<form action="?" method="post">
<div>
<input type="" name="id" value="<?php
htmlout($name['id']); ?>">
<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Delete">
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<p>New search</p>
<p>Return to JMS home</p>
</body>
</html>
BELOW IS THE HTML FOR THE FORM WHERE THE VALUES ARE ADDED.
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Manage Profiles</title>
</head>
<body>
<h1>Manage Profile</h1>
<p>Add new profile</p>
<form action="" method="get">
<p>View player profiles satisfying the following criteria:</p>
<div>
<label for="name">By name:</label>
<select name="name" id="name">
<option value="">Any name</option>
<!-- populates the drop down with names -->
<?php foreach ($names as $name): ?>
<option value="
<?php htmlout($name['id']); ?>">
<?php htmlout($name['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="text">Containing text:</label>
<input type="text" name="text" id="text">
</div>
<div>
<input type="hidden" name="action" value="search">
<input type="submit" value="Search">
</div>
</form>
</body>
</html>
Any help is greatly appreciated.
Thanks
The reason is the line break you have in your form control:
Change
<option value="
<?php htmlout($name['id']); ?>">
<?php htmlout($name['name']); ?>
</option>
To
<option value="<?php htmlout($name['id']); ?>">
<?php htmlout($name['name']); ?>
</option>