Ok, so I'm making a somewhat simple search feature that connects to a MySQL server.
From my understanding the way to do this is to:
Make a search page on the site.
The search page will then accept a query and run it in the .php you provide
A result page will come out.
This is the basic stock code i will use:
<html>
<body>
<form action="searchtemplate.php" method="post">
Part ID: <input type="text" name="partID" />
<input type="submit" /></form>
</body>
</html>
I got this from the w3 school site.. it's simple and it works.
The php file i'm using is:
<?php
/**
Template Name: My Template
*/
get_header(); ?>
<div class="clearfix left-sidebar">
<!--BEGIN #primary-->
<div id="primary">
<?php the_post(); ?>
<?php $con=mysqli_connect("server","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
}
$part_query = $_POST['partID'];
$result = mysqli_query($con,"SELECT * FROM Parts WHERE part_id = $part_query");
echo "<table border='1'>
<tr>
<th>part_id</th>
then rest of columns
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['part_id'] . "</td>";
then echo rest of columns
echo "</tr>";
}
echo "</table>";
mysqli_close($con); ?>
<!--END #primary-->
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer();
?>
Ok, so I am using wordpress and I set this php up in the main theme directory and it can be accessed as a template. My main problem is that the search page appears fine but when I click submit it takes me to a blank page and not to the result page. I'd like to know if this is the correct way of doing what i am trying to accomplish or if there is a better way.
Your form is likely posting to the template, searchtemplate.php, and not a Wordpress page using the template to execute the code.
<form action="searchtemplate.php" method="post">
The "action" parameter in your form tag should point to the actual page on your site that is using your custom template.
Related
So I am currently trying to create a portion of my website that fetches a mySQL table. I wrote the php to fetch the data, create a table, and print out the table in a separate php file named display.php. Now i am trying to load it into a div on my html page but for some reason it is not working. I am using XAMPP and php is working as my login page works. Here is the code:
HTML: (I have tried both include and require)
<html>
<body>
<div id="display" class="myCustom1">
<?php require('display.php'); ?>
</div>
<div id="display" class="myCustom1">
<?php include("display.php"); ?>
</div>
</body>
</html>
PHP: (if i run just the php page it creates the table)
<?php
$servername = "localhost";
$username = "test";
$password = "test";
$database = "data1";
//create connection & access DB
$connect = mysqli_connect("$servername","$username","$password","$database");
//if error, display error
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($connect,"SELECT * FROM data1enter");
echo
"
<table border='1'>
<tr>
<th>Song</th>
<th>Author</th>
</tr>
";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['song'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($connect);
?>
You are using <?php ?> tags in the html file, Change the extension of the file containing the below code to .php from .html and you will be able to use include function.
<html>
<body>
<div id="display" class="myCustom1">
<?php require('display.php'); ?>
</div>
<div id="display" class="myCustom1">
<?php include("display.php"); ?>
</div>
</body>
</html>
Also make sure the include path is correct.
Hope it helps !
Display should be blade template ..
#include('display')
Give path to display template
Just put the php in a function. Then you can do this:
<html>
<body>
<?php require_once('display.php'); ?>
<div id="display" class="myCustom1">
<?php render(); ?>
</div>
</body>
</html>
<?php
function render()
{
//YOUR PHP CODE
}
?>
I got myself into a project at work that is going beyond my (very modest) coding skills.
So i have a database with 6 fields. I'm able to make searchs on it using php (i followed a youtube tutorial), add records, delete records and update records. All of that is working just fine.
What i need now is this:
I need to link search results (only a few, the most popular) on a image.
Per example, i have a company logo on my main page (let's say company name is..i dunno.. "Google". When i click on it, i wanted it to redirect to the search results of "google", like if i had inserted that in the search field. Is that possible?
(notice that i don't have companies in my database, i'm just trying to give an example so that people can understand what i intend. In my database i would need to link to reflect search results of the field "unidade")
Here is my code so far. The add / update .php i don't think are needed.
<?php
// include the connection file
include "connection.php";
$sql = "SELECT * FROM usuariotb";
if (isset($_POST['search'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .=" WHERE nome LIKE '%".$search_term."%'";
$sql .=" OR posto = '{$search_term}' ";
$sql .=" OR nim = '{$search_term}' ";
$sql .=" OR unidade LIKE '%".$search_term."%'";
$sql .=" OR codigoueo LIKE '%".$search_term."%'";
}
$query = mysql_query($sql) or die (mysql_error());
if (isset($_GET['recordId'])) {
$id = mysql_real_escape_string($_GET['recordId']);
$sql_delete = "DELETE FROM usuariotb WHERE id = {$id}";
mysql_query($sql_delete) or die(mysql_error());
header("location:display_data.php");
exit();
}
?>
<html>
<head>
<meta charset="iso-8859-1">
<title></title>
<link rel="stylesheet" type="text/css" href="format.css" />
</head>
<body>
<div class="container">
<form name="search_form" method="POST" action="display_data.php">
Search: <input type="text" name="search_box" value=""/>
<input type="submit" name="search" value="Procurar">
</form>
<div class="container">
<div class="content">
<div class="toolbar">Adicionar Novo Militar</div>
</div>
<div class="toolbar">Voltar à página de pesquisa</div>
</div>
</div>
<div class="container">
<div class="content">
<table width="90%" cellpadding="5" cellspacing="5">
<tr>
<td><strong>Nome</strong></td>
<td><strong>Nim</strong></td>
<td><strong>Posto</strong></td>
<td><strong>Unidade</strong></td>
<td><strong>Sigla Unidade</strong></td>
<td><strong>Observações</strong></td>
<td><strong>Acções</strong></td>
</tr>
<?php if (mysql_num_rows($query)) { ?>
<?php while ($row=mysql_fetch_array($query)) {?>
<tr>
<td><?php echo $row['nome'];?></td>
<td><?php echo $row['nim'];?></td>
<td><?php echo $row['posto'];?></td>
<td><?php echo $row['unidade'];?></td>
<td><?php echo $row['codigoueo'];?></td>
<td><?php echo $row['observacoes'];?></td>
<td>Delete</td>
<td>Edit</td>
</tr>
<?php } /* end loop */ ?>
<?php } else { ?>
<h2> Nothing to display!</h2>
<?php } /* end rows checking */ ?>
</table>
</div>
</div>
</body>
</html>
First of all, I empathize with you regarding being tasked with a more complex task than your current ability. Been there, done that. Many companies do this.
Regarding your task, for search every website usually uses GET to do the search e.g. https://www.google.co.in/search?q=stackoverflow. Notice the q=stackoverflow part. For this you do not need to submit any form, just use the URL directly.
So, I suggest you visit the websites where you want to link directly for search. Search for some sample text, say TEST and observe the URL of the page that opens.
Then using JavaScript/jQuery/etc, on click of the logo, read whatever text you want to search, create a URL in JavaScript and redirect there.
This is the general idea, feel free to comment and ask if you face any issues in implementation.
I am trying to make a website and it's almost completed but I want to add a view counter so when someone visit the page it count the view and save it into the database.
My script is working fine but the problem is that it continue view count even visitor is viewing anyother page
My pages url show like this
pictures.php?ID=13
I have added this PHP code in *count.php*
<?php
session_start();
if (isset($_SESSION['views'])){
$_SESSION['views']++;
} else {
$_SESSION['views'] =0;
}
//echo $_SESSION['views'];
?>
Page *views.php*
<?php
session_start();
if (isset($_SESSION['$post_id'])){
$_SESSION['$post_id']++;
} else {
$_SESSION['$post_id'] =0;
}
//echo $_SESSION['views'];
?>
<?php
echo "<hr><div align=\"center\">";
echo $_SESSION['$post_id'];
?>
<?php
$save = $_SESSION['$post_id'];
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE save_data SET Views='$save' WHERE ID='$page_id'");
mysqli_close($con);
?>
And added this line in Pictures.php where I want to show and count visits
<?php include("views.php"); ?>
Problem:
When someone visits page pictures.php?ID=8 it will show him page view 1 and save this view in database where ID=8, when he visit page pictures.php?ID=12 it will show him view 2 and save this 2 in database where ID=12. My point is that it is continuously counting instead of each page view.
Thanks in advance
Here is Pictures.php
<?php
include("connection.php");
if(isset($_GET['ID'])){
$page_id = $_GET['ID'];
$select_query = "select * from save_data where ID='$page_id'";
$run_query = mysql_query($select_query);
while($row=mysql_fetch_array($run_query)){
$post_id = $row['ID'];
$post_title = $row['Title'];
$post_image = $row['Name'];
?>
<h3>
<a href="pictures.php?ID=<?php echo $post_id; ?>">
<?php echo $post_title; ?>
</a>
</h3><center>
<form id="search-form" action="javascript:void(0);">
<input type="text" id="dimen" name="dimension" />
<input type="submit" value="Resize" Onclick ="splitString()"/></form>
<div id="sizet">
Type size like 200*300 in box
</div></center>
<div id="img"><img id="myImage" src="uploads/<?php echo $post_image; ?>" /></div>
<?php } }?>
<center>
<div id="postdetails">
<?php include("posted_by.php"); ?></center>
</div>
<?php include("views.php"); ?>
<html>
<link href="css/Pictures.css" rel="stylesheet" type="text/css">
<body>
<head>
<script type="text/javascript">
function splitString()
{
var myDimen=document.getElementById("dimen").value;
var splitDimen = myDimen.split("*");
document.getElementById("myImage").width=splitDimen[0];
document.getElementById("myImage").height=splitDimen[1];
}
</script>
</head>
</body>
Variables inside of single quotes are not evaluated, so regardless of whether $post_id is 8 or 12, $_SESSION['$post_id'] is setting the key named literally $post_id, rather than the key named 12 or 8. Variables are evaluated inside double quotes, so $_SESSION["$post_id"] would work, but the simplest and best way is to use $_SESSION[$post_id] instead.
Additionally, using $_SESSION here is probably not what you want to do. $_SESSION will be different for every user who visits the site, so when a new visitor comes to the site, it will start over with a count of 1. What you probably want to do is load the views value from the database, add one to it, and then save it back to the database. $_SESSION is for keeping data that is specific to a certain user.
Try to use structure like this
$_SESSION['view'][{resource_name}_{resource_id}]
E.g. for picture with id 8 it will be
$_SESSION['views']['picutures_8']++
I have a PHP page with HTML content in it. Now I run some PHP codes between HTMLs and I get some results. The fact is, when I try to get a respond from this page by AJAX it'll show me the whole page content and plus the result I was looking for. What can I do to prevent the page from writing extra content. I know whenever something get printed on page it'll go as respond to the AJAX call but I want a way to somehow fix this.
My page file (name: page.php):
<?php echo $Content->GetData('HEADER'); ?>
<div id="Content">
<div id="Page">
<?php if($Content->GetData('PAGE','IS_TRUE')) : ?>
<?php if(NULL !== $Content->GetData('PAGE','TITLE')) : ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeTitle"); ?>
<div id="Title" dir="rtl">
<?php echo $Content->GetData('PAGE','TITLE'); ?>
</div>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterTitle"); ?>
<?php endif; ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeContent"); ?>
<?php echo $Content->GetData('PAGE','CONTENT'); ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterContent"); ?>
</div>
</div>
<?php else : ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
There is no page like this in our archives.
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php echo $Content->GetData('FOOTER'); ?>
If I write an address in my browser like this localhost/cload/blog?action=rate it'll go through my redirection list and show the page.php with blog plugin loaded. The problem is I want to call my blog plugin by AJAX through this address but it will first render the page data.
Sorry if this is messy.
I'd suggest modifying page.php to be some functions, primarily a data processing function, and then an output function. Then, when you load the page, put a check in for whether it's an AJAX request, and if so, echo the data you want as JSON, otherwise render the page using the output function.
Alternatively, you could create a second, separate page, but that could be more difficult to maintain than a single file.
Yes, you should check whether your request is a ajax request, if it is so you should change your response to return only the result whatever want.
This php code will give an rough idea on this.
if($request->isXmlHttpRequest()){
return new Response(json_encode($data_array));
}
Hope this will help.
i m trying to display an image stored in a database by passing the image_id(an auto increment field) into the query string and then including imagedisplay.php(file used to display the image) in image tag. The problem is that this file, imagedisplay.php is not access ie the control never seems to go into the file. The code for both the main file and the imagedisplay.php file are below. Please note that i m not able to write the head part of the code here due to some formatting issue. The head section is standard html section pre-formatted in Dreamweaver.
<body>
<div class="page shadow-round">
<div id="header">
<div id="logo">
<script type="text/javascript" src="../js/header.js"></script>
</div>
</div>
<div id="menu">
<script type="text/javascript" src="../js/navmenu.js"></script>
<script type="text/javascript">
</script>
</div>
<div class="content overflow" style="height:900px;">
<?php
require_once 'login.php'; //contains the classes for connecting to databases
$dbh=new DB_Mysql; //executing queries
$func=new DB_Mysql_code_functions;
session_start();
if(isset($_SESSION['username']))
{
echo<<<_END
<form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
<table width="990">
<tbody>
<tr><td>Select an image file to be uploaded:</td></tr>
<tr><td><input type="submit" value="UPLOAD" /></td></tr>
</tbody>
</table>
</form>
_END;
if(isset($_FILES['imagefile']['tmp_name']))
{
$imagefile=$_FILES['imagefile']['tmp_name'];
$image_size=$_FILES['imagefile']['size'];
$image_name=addslashes($_FILES['imagefile']['name']);
$image_data = addslashes(file_get_contents($imagefile));
$image_array=getimagesize($imagefile);
$image_type=$image_array['mime'];
$image_height=$image_array[1];
$image_width=$image_array[0];
$maxfilesize=2000000;
if($maxfilesize<$image_size)
{
echo "Please upload a smaller image. The size of the image is too large.";
}
else
{
$query="INSERT INTO gallery(image_name,image_type,image,image_size) VALUES ('". $image_name."','".$image_type."','".$image_data."','".$image_size."')";
$stmt=$dbh->execute($query);
$lastimageid=mysql_insert_id();
echo "<p>You uploaded this image</p>";
echo "<img src='imagedisplay.php?imageid=".$lastimageid."' />";
}
}
}
else
echo "<br/><br/> Your are <span class=\"red\"><b>not Authorized</b></span> to view this page. If you are the Admin, please login with your credentials again. <a href='login_page.php'>Click here to continue</a>";
?>
</div>
</body>
</html>
Now the problem is that the control never goes to imagedisplay.php ie. it fails to reference imagedisplay.php altogether.
the code for imagedisplay.php is below:
<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;
$id=$_GET['imageid'];
$query="SELECT * FROM gallery where image_id=".$id;
$stmt=$dbh->execute($query);
$row=$stmt->fetch_row();
$imagedata=$row[3];
header("Content-type:image/jpeg");
echo $imagedata;
?>
I have tried all permutation combinations with the quotes, tried echo statements to see if control enters the file....but it does not...it stays in the main file only...i dont understand the reason...please help...please don't worry about SQL injections as i plan to deal with them later, once the code starts working....thanks
Your echo command should be like this:
echo '<img src="imagedisplay.php?imageid=' . $lastimageid . ' " />';
Look at your generated HTML source and make sure it's right. Check your error log to see if you are getting any errors from your image script.