Making html page appear on click of submit button - php

I am trying to make another page to appear when I click certain button, I basically know how to do this, but this time I am in trouble.I have following code:
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {
?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php'>
<input type="button" name = "saada" value="saada">
<input type="button" value="tulemused">
<input type="button" value="lõpeta ennetähtaegselt">
<input type="button" value="X">
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
<?php
if(isset($_POST['saada'])){
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/saada.html'>";
}
?>
Everything works just fine but when I click button 'saada' nothing happens. What should I do to make saada.html appear on this click?

replace this:
<form action='Minu_kysitlused_1.php' method="post">
Missing:
method="post"

why you try with meta tag
You just put header("location:yourpagename");
just like below
if(isset($_POST['saada'])){
header("Location:http://localhost/Praks/saada.html");
}

try like this
header function must be First thing to sent as html otherwise it will not work
<?php
if(isset($_POST['saada'])){
header( "Location : localhost/Praks/saada.html");
die();
}
?>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {
?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php'>
<input type="button" name = "saada" value="saada">
<input type="button" value="tulemused">
<input type="button" value="lõpeta ennetähtaegselt">
<input type="button" value="X">
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
second approach
<form action="your-url">
<input type="submit" name="Submit" value="saada"/>
</form>

in first time, I don't understand why do you use the form tag to call a page, if there aren't any data passed via POST?
Anyway, in your foreach cicle you can use a very simple collection of link:
foreach($titles as $title) {
echo $title;
echo 'Link 1';
echo 'Link 2';
echo 'Link 3';
echo 'Link 4';
}
This solution is too clean.
Bye
Marco

Related

Deleting multiple rows using checkbox

I have this code
<html>
<head>
<meta charset="UTF-8">
<title> Game Library</title>
<link href="css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div id="search">
<img id="del" src="img/delete.jpg" alt="Error">
<a class="A" href="index.php"><img class="AR" src="img/src.jpg" title="Search library"></a>
<a class="A" href="insert.php"><img class="AR" src="img/add.png" title="Add game"></a>
<div id="results">
<?php
require("inc/connection.php");
$query = "SELECT * FROM Library";
$result = mysqli_query($conn,$query);
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
?><div id="results">
<form action="inc/delete.php" method="GET">
<input type="checkbox" name="checkbox[]" value="<?php echo $row['ID'] ?>">
<p id="p1">Name: <?php echo $row['Name']?>;</p>
<p>Genre: <?php echo $row['Genre']?></p>
<p>Release date: <?php echo $row['Release_date']?></p>
<p>Publisher: <?php echo $row['Publisher']?>:</p>
<p>Platforms: <?php echo $row['Platforms']?></p>
</div>
<?php
}?>
<input type="submit" name = "submit" value = "Submit"></form>
<?php
}else{
echo "No results!";
}
</div>
</div>
</div>
</body>
</html>
And I am supposed to create action="inc/delete.php" script which will allow me to delete multiple rows using checkbox and submit button. Can someone help me out with this please ? I honestly have no clue on how to complete this ....
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['checkbox']))
{
foreach ($_POST['checkbox'] as $key => $check)
{
// delete query
}
}
}
?>
NOTE: You failed to close PHP at line no. 40. Please make sure your PHP tag is closed or not.
Need to first set form method to `POST'
So, modify
<form action="inc/delete.php" method="GET">
To:
<form action="inc/delete.php" method="post">
And in inc/delete.php,
if (isset($_POST['submit'])){ // Check if form is submitted.
if (! empty($_POST['checkbox'])){ // Check if Checkbox is checked.
// Loop over the checkbox and get selected ids.
foreach($_POST['checkbox'] as $checkbox_id){
echo $checkbox_id."<br/>";
// Add your delete query here.
}
}
}
So the resolution is that you get at your delete script array of ids. You can parse that array and execute DELETE query for each id:
if(isset($_POST['checkbox']))
{
foreach($_POST['checkbox'] as $val)
{
$stmt = $conn->prepare("DELETE FROM Library WHERE id = ?");
$stmt->bind_param('i', $val);
$stmt->execute();
}
}
inc/delete.php
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['checkbox'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['checkbox'] as $row_id){
echo $row_id."</br>";
// DELETE QUERY HERE
}
}
}
?>

HTML / PHP / SQL How to display a button under certain circumstances?

So I have a script using HTML, PHP, and mysql, and I want to display a button under certain circumstances.
Here is my script:
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$count = mysqli_num_rows($query);
if($count != "1"){
$output = '<h2>No result found!</h2>';
}else{
while($row = mysqli_fetch_array($query)){
$s = $row['name'];
$output .= '<h2>Found: '.$s.'</h2><br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php echo $output; ?>
</body>
</html>
Specifically, I want to display the return button only when the output is "No results found", when the amount of rows in the SQL database matching the given query is not 1. How could I go about accomplishing this? I'm relatively new to PHP and mySQLi, but from my research I couldn't figure out how to do such a task, any ideas?
<?php
if ($count==0) {
echo '<input type="submit" name="return" value="Return">';
}
?>
If you want a much cleaner html code, do this:
<form method="POST" action="index.html">
<?php if ($count!= "1") : ?>
<input type="submit" name="return" value="Return">
<?php else : ?>
<!-- put your other button here -->
<?php endif; ?>
</form>
You can read more about escaping from HTML here.
<?php
include_once('dbconnect.php');
$q = $_POST['q'];
$q = $_GET['query'];
$query = mysqli_query($conn,"SELECT * FROM `Persons` WHERE `id` LIKE '%$q%'");
$results = mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="POST" action="index.html">
<input type="submit" name="return" value="Return">
</form>
<?php if(0 < count($results)) ?>
<?php foreach($results AS $row) : ?>
<h2><?= $row['name'] ?></h2>
<?php endforeach; ?>
<?php else : ?>
<H2> No results found!</h2>
<?php endif; ?>
</body>
</html>

Print out table details from database

I am trying print out a table that has student testimonials and I want to print out the ID, title, text and student ID but when hit the "search_all" button, I am told that it can't fetch mysqli and I am not sure what to do.
<?php ("session.php"); ?>
<?php require_once("connect.php"); ?>
<?php include("header.php"); ?>
<html>
<body>
<!-- Page Content -->
<h1 class="page-header">Learning Journals
<small>- Admin Search</small>
</h1>
<h3>Search Learning Journals</h3>
<form name="membership_form" action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="contactForm" method="post">
<label>Search Journals ID:</label>
<br>
<input type="text" name="journal_search">
<br>
<button type="submit" name ="search_all" class="btn btn-primary">All Journals</button>
</form>
<?php
if(isset($_POST["search_button"]))
{
//create the sql statement
$sql_all= "SELECT * FROM testimonials";
$result = mysqli_query($con,$sql_all);
while ($row = mysqli_fetch_assoc($result))
{
echo
"<h4>Testimonial ID:</h4>". $row["testimonial_ID"].
"<h4>Title:</h4> ". $row["testimonial_title"].
"<h4>Text:</h4> ". $row["testimonial_text"].
"<h4>Student ID:</h4>". $row["student_ID"].
"</br>";
}
mysqli_close($con);
}
include("footer.php");
?>
</body>
</html>

PHP - Wrong data is passed and shown to another page

this is what i want to do, i have two pages, first is the "edit.php" and second is the "edit2.php". the page "edit.php" this is the page where all the news is shown and you will select what news you will edit by clicking the "edit button" and the "edit2.php" where will i exactly edit the news. i want to pass the value of the selected news in another page. But here is the problem, when i clicked the "Sample news 1" edit button the data showing in another page is the "Sample news 2". Even when i clicked the "Sample news 2" edit button, the data is also the "Sample news 2". Can someone give me ideas on how to fix this?
here is the picture of edit.php. I click the edit button of "Sample news 1".
here is the picture of edit2.php. and this is the output data. The data should be "Sample news 1" not "Samplel news 2".
here is my php code in edit.php
<?php
session_start();
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $newsid;
$_SESSION['n_title'] = $title;
$_SESSION['n_date'] = $date;
$_SESSION['n_content'] = $content;
$_SESSION['n_image'] = $newsimage;
header('Location: edit2.php');
}
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
here is my php code for "edit2.php"
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form method="post" action ="" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $_SESSION['n_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $_SESSION['n_date']; ?>" /><br>
Content<textarea name="content"><?php echo $_SESSION['n_content']; ?></textarea>
<input type="submit" name="submit" value="Update" />
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $_SESSION['n_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
</form>
<hr>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Remove if(isset($_POST['esubmit'])){ code out of while loop and add values in form as I shown in below example
Pass $newsid in edit form as hidden and retrieve the content based on new
<form action="" method="post">
<input type='hidden' value="<?php echo $newsid;?>" name="news_id">
<input type="submit" name="esubmit" value="edit" />
</form>
and in your php add this line.
if(isset($_POST['esubmit'])){
$_SESSION['news_id'] = $_POST['news_id'];
The problem is, in each iteration of while loop you're overwriting $newsid, $title, $date,... variables. So when you submit the form, the last row's data will get stored in the corresponding $_SESSION variables.
So here's the solution to your problem.
You don't need $_SESSION to pass the form values to edit2.php page, instead change the <form> element in the following way,
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
On edit2.php page, first catch the news_id value using $_GET superglobal , like this:
$newsid = $_GET['news_id'];
And then get the appropriate news details using this $newsid, and finally populate the form.
Here's the complete code,
edit.php
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$newsid = $row['news_id'];
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
$newsimage = $row['news_image'];
?>
<div class="fix single_news">
<div class="single_image">
<img src="<?php echo $newsimage; ?>" style="width:200px; height:140px; alt="court">
</div>
<?php echo $title; ?>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<form action="edit2.php?news_id=<?php echo $newsid; ?>" method="post">
<input type="submit" name="esubmit" value="edit" />
</form>
<hr>
<?php
}
?>
edit2.php
<?php
if(isset($_POST['esubmit'])){
$newsid = $_GET['news_id'];
include_once('connection.php');
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "SELECT * FROM news WHERE news_id = ? LIMIT 1")) {
/* bind parameters */
mysqli_stmt_bind_param($stmt, "s", $newsid);
/* execute query */
mysqli_stmt_execute($stmt);
/* get the result set */
$result = mysqli_stmt_get_result($stmt);
/* fetch row from the result set */
$row = mysqli_fetch_array($result);
}
}
if(isset($_POST['submit'])){
// Write code to commit the edit details
}
?>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['esubmit'])){
?>
<form method="post" action ="edit2.php?news_id=<?php echo $row['news_id']; ?>" enctype="multipart/form-data">
Title<input type ="text" name ="title" value="<?php echo $row['news_title']; ?>"/><br>
Date<input type ="text" name="date" value="<?php echo $row['news_date']; ?>" /><br>
Content<textarea name="content"><?php echo $row['news_content']; ?></textarea>
<input class="form-control" id="image" name="image" type="file" accept="image/*" onchange='AlertFilesize();'/>
<img id="blah" src="<?php echo $row['news_image']; ?>" alt="your image" style="width:200px; height:140px;"/>
<input type="submit" name="submit" value="Update" />
</form>
<?php
}
?>
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
Here is the issue, You are looping your data to the session, usually single session keeps only a value. Since that final item of your loop will store on the session [in this case it is 'Sample news 2']. I believe, you can place it on a hidden field & post it to the next page or you can use URL Parameter [GET], to pass the Id to the next page.
Ex : <a href ='edit2.php?newsId ='<?php echo $newsid?>> Edit </a>
what I think is happening is when you are clicking edit button this edit.php page is again reloading and that sql query is running again that's why it is taking value of first image in both cases.what I suggest you to try is use $_get[] instead of $_SESSION and $_POST and pass value of image id directly to page edit2.php and query others details from database in that page and then display it.try it
Remove <form> and <input> use just <a> instead like this.
<a href='edit2.php?id=$news_id'>Edit</a>
and then fetch this id in edit2.php like following
$news_id=$_GET['id'];
and atlast fetch other details of news from the database using this id like'
$query='select * from news where news_id=$news_id';

Task-list manager app in PHP doesn't show the list of tasks

I am working on a task list manager for my class that I am in. I have to include sessions and I need to push each task into an array and be able to delete it from the array.
I think I am doing it correctly,but when I run the program, the task list shows up as empty. I think I may not be adding the tasks to the task-list array correctly, and I really would like a fresh set of eyes to take a look, because maybe you will see what I haven't.
index.php
<?php
if (isset($_POST['tasklist'])) {
$task_list = $_POST['tasklist'];
} else {
$task_list = array();
}
// put the array in a session variable
$_SESSION['tasks']=$task_list;
$_SESSION['errors']=$errors;
//start the session to last one year
$lifetime = 60 * 60 * 24 * 365; // 1 year in seconds
ini_set('session.gc_maxlifetime', $lifetime);
//session_set_cookie_params($lifetime, '/');
session_start();
$errors = array();
switch( $_POST['action'] ) {
case 'add':
$new_task = $_POST['newtask'];
if (empty($new_task)) {
$_SESSION['errors'] = 'The new task cannot be empty.';
} else {
$_SESSION['tasks'] = $new_task;
}
break;
case 'delete':
$task_index = $_POST['taskid'];
unset($task_list[$task_index]);
$_SESSION['tasks'] = array_values($_SESSION['tasks']);
break;
}
include('task_list.php');
?>
task_list.php
<?php
session_start(); // Access the current session.
?>
</head>
<body>
<div id="page">
<div id="header">
<h1>Task List Manager</h1>
</div>
<div id="main">
<!-- part 1: the errors -->
<?php if (count($_SESSION['errors']) > 0) : ?>
<h2>Errors</h2>
<ul>
<?php foreach($_SESSION['errors'] as $error) : ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- part 2: the tasks -->
<h2>Tasks</h2>
<?php if (count($_SESSION['tasks']) == 0) : ?>
<p>There are no tasks in the task list.</p>
<?php else: ?>
<ul>
<?php foreach($_SESSION['tasks'] as $id => $task) : ?>
<li><?php echo $id + 1 . '. ' . $task; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<br />
<!-- part 3: the add form -->
<h2>Add Task</h2>
<form action="." method="post" >
<?php foreach($_SESSION['tasks'] as $task) :
array_push($_SESSION['tasks'],$task);
endforeach; ?>
<!--<input type="hidden" name="action" value="add"/> -->
<label>Task:</label>
<input type="text" name="<?php $_SESSION['tasks'] ?>" id="newtask" value="<?php echo $task; ?>" /> <br />
<label> </label>
<input type="submit" value="Add Task"/>
</form>
<br />
<!-- part 4: the delete form -->
<?php if (count($_SESSION['tasks']) > 0) : ?>
<h2>Delete Task</h2>
<form action="." method="post" >
<?php foreach($_SESSION['tasks'] as $task) :
$_SESSION['tasks']=array_diff($_SESSION['tasks'],$task);
endforeach; ?>
<!--<input type="hidden" name="action" value="delete"/> -->
<label>Task:</label>
<select name="taskid">
<?php foreach($_SESSION['tasks'] as $id => $task) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<br />
<label> </label>
<input type="submit" value="Delete Task"/>
</form>
<?php endif; ?>
</div><!-- end main -->
</div><!-- end page -->
</body>
</html>
You need to call session_start() before any interaction with the session. So if the files are accessed separately then you need it in both. If task_list.php is only included in index.php then just move it to the top of index.php. Additionally, in your logic for the add action you dont have your syntax right to add a task to $_SESSION['tasks'] array, instead you are overwriting it with whatever is in $new_task.
This is from Murach's PHP book. The book becomes bunk after like the 10th chapter. No valid explanations or examples and questions that assume you already know PHP because it doesnt teach you what you need to know.

Categories