Adding posts to from form database categories - php

I have a form from where i add posts to my database table and i made categories in this table and i puted in the "ADD POSTS" form an option to choose the category when i add the post. It works in my other website witch is just to try some new things but to my main project it doesnt and i use the same code with same tables names. Can someone check the code and tell me where is my mistake?
Thats my whole ADDposts form:
<?php
include 'includes/connect.php';
if(isset($_POST['add']))
{
$time = time();
$title = htmlspecialchars($_POST['title']);
$content = strip_tags($_POST['content']);
$post_image= $_FILES['image'] ['name'];
$image_tmp= $_FILES['image'] ['tmp_name'];
$q = "INSERT INTO posts(post_title,post_content,post_author,added,post_image) VALUES('$title','$content','Papazov','$time','$post_image')";
mysql_query($q) or die (mysql_error());
}
?>
<!DOCTYPE html>
<?php include "hhh.html"; ?> <br /><br />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MatchZone</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<p>
<form method="post" action="Addpost.php" enctype="multipart/form-data">
Заглавие: <input type="text" name="title" /><br/><br/>
<tr>
<td align="right">Preview:</td>
<td><input type="text" name="preview" size="30"></td>
</tr>
<br />
<br />
<tr>
<td align="right">Сложи снимка: </td>
<td><input type="file" name="image"></td>
</tr>
<br/><br/>
Категория: <select name="category">
<?php
$q = mysql_query("SELECT*FROM categories") or die (mysql_error());
while($c = mysql_fetch_assoc($q))
{
print '<option value="'.$c['cat_id'].'">'.$c['name'].'</option>\n';
}
?>
</select><br />
<br />
<textarea class="ckeditor" name= "content" cols="30" rows="10"></textarea><br />
<input type="submit" name="add" value="Добави" />
</form>
</p>
</body>
</html>

Did you close the select tag? If you can show the part where you insert to the database would be of help

This looks to me like you're trying to run everything off the same page.
If this is the case (which most likely is), then change action="Addpost.php" to action=""
Since you have if(isset($_POST['add'])) near the top of your code,
and further below <input type="submit" name="add" value="Добави" /> clearly being in the same page.

Related

Why are my search results not visible under the search form?

hoping somebody is able to help!
I have created a search form to enable a user to search for a specific assessment day using the name, date and the company it is for.
The results of this search should appear below the form used to generate the search. However, at the moment, when the search button is selected the form just refreshes and does not show any results?
Can anyone advise? Code below:
<?php
if(isset($_GET['submit'])){
require_once 'connect.php';
if(isset($_GET['nameofassessmentday'])) {
if(isset($_GET['dateofassessmentday'])) {
if(isset($_GET['companyname'])) {
$nameofassessmentday = $db-> real_escape_string($_GET['nameofassessmentday']);
$dateofassessmentday = $db-> real_escape_string($_GET['dateofassessmentday']);
$companyname = $db-> real_escape_string($_GET['companyname']);
$query = $db->query ("
SELECT Name, Company
FROM assessment_day_details
WHERE Name LIKE '{$nameofassessmentday}'
AND Company LIKE '{$companyname}'
AND Date_of_Day = '{$dateofassessmentday}'
");
}
}
}
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
if($query-> num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->Name; ?>
<?php echo $r->Company; ?>
</div>
<?php
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Assess Existing Assessment Day-Search</title>
<link rel="stylesheet" href="style.css">
</head>
<?php
include 'function.php';
}
?>
<body>
<div id="form">
<form method="get">
<p>
<label>Name of Assessment Day:</label>
<input type="text" id="nameofassessmentday" name="nameofassessmentday" required/>
</p>
<p>
<label>Date of Assessment Day:</label>
<input type="date" id="dateofassessmentday" name="dateofassessmentday" required />
</p>
<p>
<label>Company Name :</label>
<input type="text" id="companyname" name="companyname" required/>
</p>
<p>
<input type="submit" id="btn" value="Search" />
</p>
</form>
</div>
</body>
</html>
Also, just one additional question for future reference. Is there anyway I would be able to hyperlink a search result to go to a particular page?
Thanks in advance!
And as some further information, prior to trying to get the search results to show in the same webpage, I had the below two pages and it worked perfectly.
<!DOCTYPE html>
<html>
<head>
<title>Assess Existing Assessment Day-Search</title>
<link rel="stylesheet" href="style.css">
</head>
<?php
include 'function.php';
?>
<body>
<div id="form">
<form action="search.php" method="get">
<p>
<label>Name of Assessment Day:</label>
<input type="text" id="nameofassessmentday" name="nameofassessmentday" required/>
</p>
<p>
<label>Date of Assessment Day:</label>
<input type="date" id="dateofassessmentday" name="dateofassessmentday" required />
</p>
<p>
<label>Company Name :</label>
<input type="text" id="companyname" name="companyname" required/>
</p>
<p>
<input type="submit" id="btn" value="Search" />
</p>
</form>
</div>
</body>
</html>
<?php
require_once 'connect.php';
include 'function.php';
if(isset($_GET['nameofassessmentday'])) {
if(isset($_GET['dateofassessmentday'])) {
if(isset($_GET['companyname'])) {
$nameofassessmentday = $db-> real_escape_string($_GET['nameofassessmentday']);
$dateofassessmentday = $db-> real_escape_string($_GET['dateofassessmentday']);
$companyname = $db-> real_escape_string($_GET['companyname']);
$query = $db->query ("
SELECT Name, Company
FROM assessment_day_details
WHERE Name LIKE '{$nameofassessmentday}'
AND Company LIKE '{$companyname}'
AND Date_of_Day = '{$dateofassessmentday}'
");
}
}
}
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
if($query-> num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->Name; ?>
<?php echo $r->Company; ?>
</div>
<?php
}
}
?>
you have to add '%' when you use LIKE in query
check here https://www.w3schools.com/sql/sql_like.asp

Display text after submit button is clicked. PHP

I am trying to catch some text from the textbox, to save it in a variable and then post it on my browser if I click the submit button. My code is this:
<?php
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</body>
</html>
I don't know what is wrong. The code on rextester
For starters you need to implement a form on you HTML page and set the action to a new .php page.
<form action='display.php' method='post'>
*your input fields go here*
Now create a new php page (in this case display.php)
Declare the variables as you did....
$var1=$_POST['text1'];
$var2=$_POST['display'];
if(isset($var2)){
var_dump( $var1);
then you can simply echo each variable accordingly...
Final Result
HTML PAGE:
<form action='result.php' method='post'>
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
result.php
<?php
$var1 = $_POST['text1']; // create variables of your form elements
$var2 = $_POST['display'];
echo '$var1 . ' ' . $display . '.';
?>
Basically the php page is saying get text1 and display from the form (names) and create variables of them...
Then echo (print) these variables on the screen. (in plain english xD)
p.s
Your rextester isn't working because you haven't specified a form.
You need to setup the FORM tags so they have the correct attributes for POST. Try:
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="submit" id="display" name="display"><br /><br />
</form>
</body>
</html>
You need to use a form, otherwise you aren't posting anything.
Something like:
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form method="post" action="yourphpfile.php">
<input type="text" value="gdf" name="text1" id="text1" /><br /><br />
<input type="text" value="display" name="display" id="display" /><br /><br />
<input type="submit" value="submit"><br /><br />
</form>
</body>
</html>
yourphpfile.php
<?php
if(!empty($_POST['text1']) and !empty($_POST['display'])){
$var1=$_POST['text1'];
$var2=$_POST['display'];
echo "$var1 $var2";
}
?>
Check this complete example
To display text box and button on clicking submit button
<form>
<input type="submit" value="OK" name="btn_name"/><br />
<?php
if(isset($_GET['btn_name']))
{
echo "<input type=text name=txt_userinput />";
echo "<input type=submit value=Area name='btn_calculate'/>";
}
?>
</form>

Form action not refreshing properly

I have a problem whit form submission. I have a html form and the action simpli doesnt want to work. maybe i am doing something wrong, but here take a look at it:
<form action="add.php" method="post" autocomplete="off">
<input type="text" name="title" placeholder="Title" style="width:300px;">
<select id="type" name="type">
<option value="0">Matrix</option>
<option value="1">LoL</option>
<option value="2">Dota 2</option>
</select><br />
<textarea rows="30" cols="90" placeholder="Content here" name="content"></textarea><br />
<input type="submit" value="DODAJ">
</form>
so when i click on the button i does refresh the page but nothing is displayed. I't worked in local but when i upload it online nothnig, just a blank page. And the stuff that has to go to the database doesnt go. no errors, no nothing.
help?
Here's the complete code for that add.php page:
<?php
include '../php/db/init.php';
include '../php/db/connection.php';
protect_page();
admin_protect();
if(isset($_SESSION['logged_in'])){
if(isset($_POST['title'], $_POST['content'], $_POST['type'])){
$title = $_POST['title'];
$content = nl2br($_POST['content']);
$content_short = substr($content,0, 150);
$type = $_POST['type'];
echo $type;
die();
if(empty($title) or empty($content) or empty($type)){
$error = 'Potrebno je popuniti sva polja!';
} else {
$query = $pdo->prepare('INSERT INTO articles (article_title, article_content, article_content_short, article_timestamp, article_type) VALUES (?,?,?,?,?)');
$query->bindValue(1,$title);
$query->bindValue(2,$content);
$query->bindValue(3,$content_short);
$query->bindValue(4,time());
$query->bindValue(5,$type);
$query->execute();
redirect('http://www.matrixgamingns.com/admin/index.php');
}
}
?>
<html>
<head>
<title>Matrix Gaming Admin Panel</title>
<link rel="stylesheet" type="text/css" href="../css/admin.css">
<link rel="stylesheet" type="text/css" href="../css/reset.css">
</head>
<body>
<div id="home">
<div id="wrapper">
<?php include 'adminmenusidebar.php'; ?>
<div id="field">
<h3 style="font-size:22px;">Dodaj vest</h3>
<?php if(isset($error)){ ?>
<small style="color:#aa0000"> <?php echo $error?> </small>
<br/>
<?php }?>
<br />
<form action="add.php" method="post" autocomplete="off">
<input type="text" name="title" placeholder="Title" style="width:300px;">
<select id="type" name="type">
<option value="0">Matrix</option>
<option value="1">LoL</option>
<option value="2">Dota 2</option>
</select><br />
<textarea rows="30" cols="90" placeholder="Content here" name="content"></textarea><br />
<input type="submit" value="DODAJ">
<?php
ini_set('display_errors','On');?>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
} else {
redirect('http://www.matrixgamingns.com/admin/index.php');
}
?>
Get rid of the die() after echo $type;
By including a die() function, it stops all in its tracks, just like return does and nothing gets executed afterwards.
You could use the login form example posted here: HTML Dynamic Forms Generator
I downloaded the source code for the login form and it saved me lots of time.

Textbox problem with onclick #

I'm just trying to display a table in client side from database as a distinct value from every column. Here i have fetched the value by using "select distinct ...." code and put this value in a single textbox
here is the code...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function onClick()
{
alert(document.form.thirdparty.value);
}
</script>
</head>
<body>
<p> </p>
<form name="form" method="get" >
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('database',$con);
$res = mysql_query("SELECT count(*) as count FROM tablename") or die(mysql_error());
while ($row = mysql_fetch_array($res)) {
$val=$row['count'];
} ?>
<input name="fino" type="text" id="fino" size="5" value="<?php echo $val." docs" ?>" style="border-style:hidden; color:#0083fa;" />
<table width="1474" height="270" border="0" >
<tr>
<td width="180" height="41">
<input name="Item" type="DocumentType" id="textfield5" value="    Item"size="30" /> </td>
<td width="180" height="41">
<label>
<td width="180" height="41"> <input type="text" name="textfield8" id="textfield8" style="visibility:hidden" /></td>
</label>
<label>
<td width="180" height="41"> <input type="text" name="textfield9" id="textfield9" style="visibility:hidden"/></td>
</label>
</td>
</tr>
<tr>
<td>
<?php
$a=mysql_query("select distinct language from tablename");
$c=0;
$i=1;
$x=1;
while($row = mysql_fetch_array($a))
{
$b=$row['language'];
$i=$b;
$d=mysql_query("select count(*) as count from tablename where language='$i' ");
while ($row = mysql_fetch_array($d)) {
$e=$row['count'];
$cal=round(($e/$val)*100);
}
?>
//in this textbox am using onclick function
<input type="text" size="30" style="height:<?php echo $cal.px?>"value="<?php echo "$i"." "." "." "."$e"." docs" ?>"name="language" id="textfield"onClick="onClick();" />
<?php
$i++;
$c++;
}
?> </td>
<td>
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('database',$con);
$a1=mysql_query("select distinct Subject from tablename");
$c1=0;
$i1=1;
while($row = mysql_fetch_array($a1))
{
$b1=$row['Subject'];
$i1=$b1;
$d1=mysql_query("select count(*) as count from tablename where Subject='$i1' ");
while ($row = mysql_fetch_array($d1)) {
$e1=$row['count'];
$cal1=round(($e1/$val)*100);
}
?>
<input type="text" size="30" style="height:<?php echo $cal1.px?>" value="<?php echo "$i1"." "." "." "."$e1"." docs" ?>"name="item" id="textfield2" />
<?php
$i++;
$c++;
}
?> </td>
<td>
<label>
<td> <input type="text" name="textfield12" id="textfield12" style="visibility:hidden" /></td>
</label>
<label>
<td> <input type="text" name="textfield13" id="textfield13" style="visibility:hidden" /></td>
</label>
<label>
<td> <input type="text" name="textfield14" id="textfield14" style="visibility:hidden" /></td>
</label>
<label>
<td> <input type="text" name="textfield15" id="textfield15" style="visibility:hidden" /> </td>
</label>
</form>
</td>
</tr>
</table>
<blockquote>
<p align="center"> </p>
</blockquote>
</body>
</html>
EDIT:
The following is a sample where the value of the textbox is displayed in an alert box.
As the others have said, cannot suggest anything other than this without seeing code.
Example
<input type = 'text' id = 'myTextBox' onclick='alert(this.value)' />
First don't call your function onclick better name is something like LanguageClick.
Second, don't give same id to all elements.. if you don't have real use for it just omit the ID it won't break anything.
Third, proper event is onfocus as user can reach the textbox by pressing Tab key, which won't trigger the click event.
Code would look like this:
<input type="text" size="30" style="height:<?php echo $cal.px?>;" value="<?php echo "$i"." "." "." "."$e"." docs" ?>" name="language" onfocus="LanguageClick(this);" />
And the JavaScript code:
function LanguageClick(oTextbox) {
var sValue = oTextbox.value;
alert("You wrote: " + sValue);
}
As you pass this to the function, you have reference to the textbox element and you can read its value property.

sticky form data problem php

Trying out some things with php and html, I'm having issues trying to get all of my form data reposted. I've figured out how to do the text boxes. Still at a loss for the select box, radio and check box. Would I be better off reposting inside the HTML page or letting my include template code take care of it?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/rocks.css">
<title>About Us-- Rocks</title>
</head>
<body>
<?php include("templates/banner_navigation.php"); ?>
<div id="links_group">
<?php include("templates/menu.php"); ?> <!-- 1st include for navigation, stored in templates -->
</div>
<div id="about_us_form">
<?php include("templates/about_us_valid.php"); ?>
<form action="<?php echo $PHP_SELF;?>" method="post">
<table>
<tr><td>First name:</td><td><input type="text" name="contact_name" title="Enter your first name here" value="<?php if (isset($_POST['contact_name'])) echo $_POST['contact_name']; ?>" /></td></tr>
<tr><td>Email Address:</td><td><input type="text" name="contact_email" title="Enter your email here" value="<?php if (isset($_POST['contact_email'])) echo $_POST['contact_email']; ?>" ></td></tr>
<tr><td>Phone Number:</td><td><input type="text" name="contact_phone_number" title="555-555-5555" value="<?php if (isset($_POST['contact_phone_number'])) echo $_POST['contact_phone_number']; ?>" /></td></tr>
<tr><td>I prefer to be contacted by:</td><td><input type="radio" name="preference" value="Email" checked/>Email<input type="radio" name="preference" value="Phone" />Phone</td></tr>
<tr><td>I am interested in:</td> <td><select name="select_rocks"><option value="gold">Gold</option>
<option value="silver">Silver</option>
<option value="thorium" selected="selected">Thorium</option>
<option value="titanium">Titanium</option>
</select></td></tr>
<tr><td> I would like to ask about:</td></tr>
<tr><td><textarea name="that_textarea" rows ="10" cols="25" title="Enter your questions here!">
</textarea></td></tr>
<tr><td>I am also interested in:</td><td>
<tr><td>Gemstones <input type="checkbox" name="checkbox1[]" value="gemstones" /></td><tr>
<tr><td>Ore processing <input type="checkbox" name="checkbox1[]" value="oreprocessing" /></td></tr>
<tr><td><input type="hidden" name="checkbox1[]" value="" /> </td><tr>
<tr><td><input type="submit" value="submit" name="submitform" /></td></tr>
</table><br/>
</form>
</div>
</body>
the include template:
<?php
function check_for_cat($namecheck=false,$emailcheck=false){
if ($namecheck) $name_field_error = "(((Name Invalid!)))";
if ($emailcheck) $email_field_error = "(((Email Invalid!)))";
//Start of form
if($namecheck) echo "<tr><td>$name_field_error</td></tr>";
if($emailcheck) echo "<tr><td>$email_field_error</td></tr>";
}
if (!isset($_POST['submitform'])) {
check_for_cat();
} else {
$namecheck = false;
$emailcheck = false;
$contact_name = isset($_POST['contact_name']) ? trim($_POST['contact_name']) : '';
$contact_email = isset($_POST['contact_email']) ? trim($_POST['contact_email']) : '';
if (strlen($contact_name)<3) $namecheck = true;
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $contact_email)) $emailcheck=true;
if ($namecheck || $emailcheck ){
check_for_cat($namecheck,$emailcheck);
} else {
echo '(((OMG the form is submitted!)))';
}
}
?>
Its probably pretty easy to do, I've just been looking at the code for too long, Thanks for the help! If possible put some code up, google and yahoo haven't found me much to look at.
For use Selected Option ...
Both radio and checkbox inputs with work with checked="checked" in order for them to be checked. You must use some php logic to determine if the user's input matches the necessary input to check the radio/checkbox and dropdown. For instance:
<input type="checkbox" <?php if ($userval == $boxval) echo 'checked="checked"'; ?> />

Categories