How to make selected value to appear in other page - php

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' method="post">
<input type="submit" 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'])){
header("Location:http://localhost/Praks/saada.html");
}
?>
Right now there is button "saada" for each title in database. When I click "saada", page saada.html will appear.
This is saada.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Saada</title>
</head>
<form action="test1.php" method="POST">
<body>
<header>Küsitluse pealkiri</header>
<br>
Lisa sõnumile pealkiri:
<br>
<textarea rows='1' name='pealkiri'>Küsitluse algne pealkiri</textarea>
<br>
Lisa Adressaadid: <br>
<textarea rows='1' name='email'></textarea>
<br>
Lisa sõnum:
<br>
<textarea rows='4' name='tekst'></textarea>
<br><footer>
<INPUT TYPE="submit" VALUE="Saada" NAME="Saada">
</form>
<FORM METHOD="LINK" ACTION="Minu_kysitlused.html">
<INPUT TYPE="submit" VALUE="Loobu">
</footer>
</body>
</html>
My question is how could I make it work so value of $title appears in saada.html. The value of this $title which is next to 'saada' button I clicked. I need this value to be in this line instead of Küsitluse algne pealkiri:
<textarea rows='1' name='pealkiri'>Küsitluse algne pealkiri</textarea>

You can not have a value through PHP in HTML type page....change page extension to .php
then pass your $title in input hidden field in your form
like this
foreach($titles as $title) {?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php' method="post">
<input type="submit" name = "saada" value="saada"/>
<input type="button" value="tulemused"/>
<input type="button" value="lõpeta ennetähtaegselt"/>
<input type="button" value="X"/>
<input type="hidden" name="title" value="<?php echo $title;?>"/>
</li>
</form>
<?php
}
?>
Now set the cookie
<?php
if(isset($_POST['saada']))
{
if(isset($_REQUEST['title']))
{
$title = $_REQUEST['title'];
setcookie("page_title", $title, time()+3600);
}
header("Location:http://localhost/Praks/saada.html");
}
?>
Now on Your saada.html you can use js to retrieve cookie like this
JS Function Reference
var title = getCookie("page_title"); // retrieve cookie
document.getElementById('page_title').value = title; // put title into textaread
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
<textarea rows='1' name='pealkiri' id="page_title" >Küsitluse algne pealkiri</textarea>

Related

Cant get image using php

I´ve been working on this project for a quite while and, the other day I was doing some code when I walk by this piece of code that keeps giving me headaches. So i got my html and php code all right but whenever i try to upload an image to my database, the image goes null, what am i doing wrong?
<?php include "connection.php"; ?>
<?php
$n=$_POST["num"];
$t=$_POST["texto"];
$i=$_POST["imagem"];
$img = mysql_query("SELECT imagem2 from segurancaofensiva where nmr=$n");
$file = $_FILES['imagem']['tmp_name'];
$image = addslashes(file_get_contents($file));
$count = $connect->query("SELECT COUNT(DISTINCT nmr) FROM segurancaofensiva")->fetch_row()[0];
if ($connect->connect_error){
die("Connection failed: " . $connect->connect_error);
}
$sql = "UPDATE segurancaofensiva SET texto='$t', imagem='{$image}', imagem2='{$image}' where nmr=$n" ;
$sql1 = "UPDATE segurancaofensiva SET texto='$t', imagem='$img' where nmr=$n";
if ($_FILES['imagem']['name']!=='' && $connect->query($sql) !== false )
{
if ($n <= $count) {
echo "actualizou\n\n";
var_dump($n);
var_dump($i);
var_dump($image);
}
else
{
echo "nao atualizou numero fora dos limites";
}
} else {
if ($connect->query($sql1) !== false){
echo "atualizou\n\n";
} else {
echo "errp";
}
}
$connect->close();
?>
<?php include 'connection.php'; ?>
<?php
$campo = $_POST['selected'];
$query = "SELECT campo FROM segurancaofensiva";
$result1 = mysqli_query($connect, $query);
$stored = $campo;
$obterquery = "SELECT * FROM segurancaofensiva where campo ='$campo'";
$x = $connect->query ($obterquery) or die ("Erro na variavel resultado");
$final = $x->fetch_array (MYSQL_ASSOC);
?>
<html>
<body>
<head>
<link rel="stylesheet" type="text/css" href="css/styleBO.css">
</head>
<div class="formulario" style="width: 100%; height: 100%;">
<form name="form2" method="POST" action="">
<h6>Campo:</h6> <select name ="selected" id="selected" >
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[0];?></option>
<?php endwhile;?>
</select>
<input type="submit" id="load" class="load" name="load" value="Carregar">
<input type="hidden" name="selectedValue" value="0"/><br>
</form>
</div>
<div class="formulario" id="form2" style="width: 100%; height: 100%;">
<form name="form1" target="apresenta" method="POST" action="menu3.php">
<label> Atualizar dados </label><br>
<h6>Texto:</h6><textarea name="texto" id="texto"><?php echo htmlspecialchars($final['texto']);?></textarea><br>
<h6>Imagem:</h6><input type="file" name="imagem"><br>
<input type="hidden" value="<?php echo htmlspecialchars($final['nmr']);?>" name="num">
<input type="submit" name="submit" value="Enviar" class="topo">
<input type="reset" value="Limpar" class="topo">
</form>
</div>
</body>
</html>
Change form html like below. PHP will not detect file object without this.
<form enctype="multipart/form-data">
Add form attribute as per your requirement.
You are missing enctype attribute in form tag
enctype='multipart/form-data'
If you post data and trying to upload file you must use enctype

How to retrieve multiple check box value using explode?

I have several check boxes and when the multiple check box is clicked & form is submitted it goes in the db search for the corresponding result but it is giving me notice :
Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\enoticeboard\home.php on line 68
this error is 3 times.
how I successfully retrieve it?
my code is:
<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
$user->redirect('index.php');
}
$user_id = $_SESSION['user_session'];
$stmt = $DB_con->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>welcome -
<?php print($userRow[ 'user_email']); ?>
</title>
<style>
.left {
margin-left: 20px;
width: 100px;
}
</style>
</head>
<body>
<div class="header">
<div class="left">
<label><a>Welcome : <?php print($userRow['user_name']); ?></a> </label>
</div>
<div class="right">
<label><i class="glyphicon glyphicon-log-out"></i> logout
</label>
</div>
</div>
<div class="content">
<h1>Notice Board</h1>
<div class="left">
<h4>Select Hostel,Branch,Year</h4>
<form action="home.php" method="POST">
<label>Hostel</label>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-1">K.P-1</input>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-2">K.P-2</input>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-3">K.P-3</input>
<br/>
<label>Branch</label>
<br/>
<input type="checkbox" name="branch[]" value="CSE">CSE</input>
<br/>
<input type="checkbox" name="branch[]" value="I.T">I.T</input>
<br/>
<input type="checkbox" name="branch[]" value="CIVIL">CIVIL</input>
<br/>
<label>YEAR</label>
<br/>
<input type="checkbox" name="year[]" value="2011">2011</input>
<br/>
<input type="checkbox" name="year[]" value="2012">2012</input>
<br/>
<input type="checkbox" name="year[]" value="2013">2013</input>
<br/>
<input type="submit" value="See Notices" />
</form>
</div>
<?php
if(isset($_POST['hostel']) && isset($_POST['branch']) && isset($_POST['year']))
{
if(!empty($_POST['hostel']) && !empty($_POST['branch']) && !empty($_POST['year']))
{
$arrayName = $_POST['hostel'];
$arrayName2 = $_POST['branch'];
$arrayName3 = $_POST['year'];
$hostel=explode(",",$arrayName);
$branch=explode(",",$arrayName2);
$year=explode(",",$arrayName3);
$stmt2=$DB_con->prepare("SELECT * FROM doc WHERE hostel=:hostel AND branch=:branch AND year=:year");
$stmt2->execute(array(':hostel'=>$hostel,':branch'=>$branch,':year'=>$year));
$userRow2=$stmt2->fetch(PDO::FETCH_ASSOC);
?>
<div class="middle">
<?php
if($userRow2 > 0){
foreach($userRow2['doc'] as $value2){
print_r($value2['doc']);
echo 'hi';
}
}
else {
echo "No records.";
}
?>
</div>
<?php
}
else {
echo "No Notice Found.";
}
}
else {
echo "Please fill in all fields.";
}
?>
</body>
</html>
explode() function splits string into array.
implode() function converts array into string.
So, it requires string as second parameter.
Your checkbox branch[] is already getting posted as array.
$_POST['branch'] is an array.
You do not need to explode() the checkbox array.
I think you need to combine array into a string.
So, use implode()
Just use:
$arrayName2 = $_POST['branch'];
$branch=implode(",",$arrayName2);
Instead of
$arrayName2 = $_POST['branch'];
$branch=explode(",",$arrayName2);
The warning is clear explode() requires parameter 2 be string, but you are passing array there. If you want to join array element with comma then you should use implode() as below :
$hostel=implode(",",$arrayName);
$branch=implode(",",$arrayName2);
$year=implode(",",$arrayName3);
Try this
if(isset($_POST['Submit'])) {
echo "<pre>";
print_r($_POST);
$checked = implode(',', $_POST['checkbox']);
echo $checked;
}

HTML form input for MySQL

I have a MySQL table named as letter and wish to insert records through the HTML data input form. Code as follows:
<head>
<meta charset="utf-8">
<link href="css/jquery-ui-1.10.1.css" rel="stylesheet">
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui-1.10.1.min.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker(({ dateFormat: "yy-mm-dd" }));
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Letter</title>
<style type="text/css" media="screen">
#import "style_contactform.css";
</style>
</head>
<?php
$querymethod = "select r_method_code, r_method from r_method order by r_method";
$resultmethod = mysql_query($querymethod) or die ( mysql_error());
$querybranch = "select bcode, branch from branch order by branch";
$resultbranch = mysql_query($querybranch) or die ( mysql_error());
$querytype = "select tcode, type from type order by type";
$resulttype = mysql_query($querytype) or die ( mysql_error());
?>
<?php
if (isset($_POST["submit"]))
{
$rno =$_POST["rno"];
$lno =$_POST["lno"];
$dol = mysql_real_escape_string($_POST["doi"]);
$hdg =$_POST["hdg"];
$from =$_POST["from"];
$address =$_POST["address"];
$method =$_POST["method"];
$type =$_POST["type"];
$branch =$_POST["branch"];
if ((empty($hdg))){
echo '<script language="javascript">';
echo 'alert("All fields must be required")';
echo '</script>';
}
else
{
$query ="INSERT INTO letter (reference_no, letter_no, date_stamp, heading, from_1, address, r_method_code, tcode, bcode) VALUES ('$rno', '$lno', '$dst', '$hdg', '$from', '$address', '$method', '$type', '$branch')";
$result = mysql_query($query) or die ( mysql_error());
$rc = mysql_affected_rows();
echo '<script language="javascript">';
echo 'alert("Added Successfully")';
echo '</script>';
}
}
?>
<html>
<form id="contactform">
<div class="formcolumn">
<label for="rno">Reference No:</label>
<input type="text" name="rno" />
<label for="lno">Letter No:</label>
<input type="text" name="lno" />
<label for="dst">Date of the Letter:</label>
<input type="text" name="dst" id="datepicker" />
<label for="hdg">Heading:</label>
<textarea name="hdg"></textarea>
</div>
<div class="formcolumn">
<label for="from">From 1:</label>
<input type="text" id="from" />
<label for="address">Address:</label>
<textarea id="address"></textarea>
<label for="method">Received Method:</label>
<select name="method" span class="al">
<?php
do {
?>
<option value="<?php echo $rowmethod['r_method_code']?>"><?php echo $rowmethod['r_method']?></option>
<?php
} while ($rowmethod = mysql_fetch_assoc($resultmethod));
?>
</select>
<label for="type">Type:</label>
<select name="type" span class="al">
<?php
do {
?>
<option value="<?php echo $rowtype['tcode']?>"><?php echo $rowtype['type']?></option>
<?php
} while ($rowtype = mysql_fetch_assoc($resulttype));
?>
</select>
<label for="branch">Branch:</label>
<select name="branch" span class="al">
<?php
do {
?>
<option value="<?php echo $rowbranch['bcode']?>"><?php echo $rowbranch['branch']?></option>
<?php
} while ($rowbranch = mysql_fetch_assoc($resultbranch));
?>
</select>
</div>
<div class="buttons">
<input class="button" type="submit" value="Submit!" />
</div>
</form>
</html>
But I was unable to add records through this form to the relevant table. I can not understand what I am going wrong. Can any one help me?... Pls...
Form's Attribute method's default value is get
You should specify it like this
<form id="contactform" method="post">
Now you can use $_POST to get data!

PHP - Processing after input is entered twice in a row

I have a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.
<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0
You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>
Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>

php Post txt file content to textarea on another page

I am writing an app to delete and modify files in a directory. I deleted files successfully. And now I want to modify and update content to the same file using textarea on another page.
<form enctype="multipart/form-data" method="post">
<div class="span7">
<table>
<thead>
<tr>
<th> List of files </th>
<tr>
</thead>
<?php
$files = glob("UploadFile/"."*");
foreach($files as $txt)
{
if(mime_content_type($txt)=="text/plain")
{
$txtname = basename($txt);
echo "<tr><td><input type='radio' name='txt' value='$txtname'/> ".$txtname."</td></tr>";
}
}
?>
</table>
</div>
<div class="span8">
<button type="submit" name="submit" value="Edit">Edit</button>
<button type="submit" name="submit" value="Delete">Delete</button>
</div>
<?php
global $txtname;
$val = $_POST["submit"];
$txtname = $_POST["txt"];
if($val=="Delete")
unlink("UploadFile/".$txtname);
else if($val=="Edit")
{
$content=file_get_contents("UploadFile/".$txtname);
header('Location:/edit.php');
/* send content to this 'edit.php' page */
}
?>
</form>
And the edit.php page, i simply checked if the code is working. It works fine.
<?php
if($_POST['append'])
{
$file_open = fopen("UploadFile/file.txt","a+");
fwrite($file_open, $_POST['append']);
fclose($file_open);
}
?>
<form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
<textarea name="append" value="">
<?php
echo $content;
$datalines = file ("UploadFile/file.txt");
foreach ($datalines as $zz)
{
echo $zz;
}
?>
</textarea>
<button type="submit" name="submit" value="save"> Save </button>
How do I get the contents of the file to textarea for modifying it.Update: I want to save changes to the same file. Thank you
<?php
if(isset($_POST['text']))
{
file_put_contents("file.txt",$_POST['text']);
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
$text = file_get_contents("file.txt");
?>
<form method="post">
<textarea name="text"><?=$text?></textarea>
<input type="submit">
</form>
with listing
if(isset($_POST['text']))
{
file_put_contents("UploadFile/".basename($_POST['file']),$_POST['text']);
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
if(isset($_GET['file']))
{
$text = file_get_contents("UploadFile/".basename($_GET['file']));
?>
<form method="post">
<input type="hidden" name="file" value="<?=urlencode($_GET['file'])?>">
<textarea name="text"><?=$text?></textarea>
<input type="submit">
</form>
<?
} else {
$files = glob("UploadFile/*");
foreach ($files as $f)
{
$f=basename($f);
?><?=htmlspecialchars($f)?><br><?
}
}

Categories