i need help to style the output when i use php and search for for example names in my database...
The output will just be default text like amateur echo...
This is my code, and a screenshot of my homepage where i want the output to come inside the div... and to be like the rest of the text?
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="contact">
<h1>Indtast postnummer & få bynavn:</h1>
<form action="select.php" method="POST">
Postnummer: <input type="text" name="formnavn"/>
<input type="submit" value="SEND"/>
</form>
<form action="index.php">
<input style="" type="submit" value="Startsiden">
</form>
</div>
<?php
$server = "localhost";
$brugernavn = "root";
$kode = "";
$db = "dbintro2f";
mysql_connect($server , $brugernavn , $kode);// or die(mysql_error());
//echo "Forbundet til mysql server<br/>";
mysql_select_db($db); //or die(mysql_error());
$postnr = filter_input(INPUT_POST, 'formnavn');
$data = mysql_query("SELECT * FROM postnummer WHERE (postnr) = $postnr" ) or die(mysql_error());
$info = mysql_fetch_array($data);
echo "Postnummer: " . $info['postnr'] . "<br/>";
echo "By: " . $info['bynavn']. "<br/>";
?>
</body>
</html>
The codes in netbeans
http://imgur.com/yNRcE56
The webpage and used paint to show how i want it to be in a div below my search field.
http://imgur.com/ZRlzb9X
Since PHP was invented as template language, you are still free to use HTML whereever inside it like:
...
$info = mysql_fetch_array($data);
?>
<div class="mydiv">
<?php
echo "Postnummer: " . $info['postnr'] . "<br/>";
...
Three lines in the middle of snippet (including closing PHP tag ?> and next opening <?php) are to output <div> on the page. When the parser encounters closing PHP tag, it stops treating the source as PHP code and starts to simply output it.
Hope it helps.
Related
I have this program that is kinda like a chat app "for testing purposes"
It works correctly but when i send a message i have to reload to make it appear on my end. I tried to redefine the text in the database after i send the message but it didnt work. I dont want to add a refresh button (i will add just to make it easier to check for if someone wrote something) , but i want the message to appear after i write it. So thought to add a refresh header in php sadly it always said that the headers were modified somewhere before so i added it before that code with a 1 sec delay , it worked but it loops. Is there any place to add the refresh header or do you have a better solution?
<?php
ini_set('session.use_cookies', 0);
$file_pointer = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$dbc = file_get_contents($file_pointer);
?>
<form action="chat.php" method="post">
<title>Sm Chat</title>
<link rel="stylesheet" type="text/css" href="stylechat.css">
<head>
<div class="nazi">
<a style="text-decoration:none" href="index.html"> Home </a>
</div>
</head>
<body vlink='white' alink='white' link='white'>
<center>
<div class="cont">
<?php
echo "<br>" . $dbc
?>
</div>
</center>
<center>
<?php
ini_set('session.use_cookies', 0);
if(isset($_POST['btn']))
{
$msg = $_POST['msg'];
$usrfile = "usr.txt";
$usr = file_get_contents($usrfile);
$raw = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$fp = fopen( $raw, 'r+');
$messg = $dbc . $usr . " : " . $msg . "<br>";
fwrite($fp,$messg);
fclose ($fp);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
}
?>
<div class="input-form">
<input type="text" value="" id="msg" name="msg" placeholder="Enter Your Message"/>
</div>
<input type="submit" value="Send" name="btn" class="btn"/>
</center>
Its best to place all PHP code on top of your page and your HTML under it.
Your HTML is not correct at all, always start with the <html> tag and add a <head> and a <body>.
I structurized your code.
You should add your refresh header as high as possible before any output is generated and also inside the $_POST statement to ensure no output is generated before it.
You should also remove all post values before refreshing to prevent the loop.
Take a look at this reference:
https://www.php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
This is how your code should look like:
<?php
ini_set('session.use_cookies', 0);
$file_pointer = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$dbc = file_get_contents($file_pointer);
if(isset($_POST['btn']))
{
$msg = $_POST['msg'];
$usrfile = "usr.txt";
$usr = file_get_contents($usrfile);
$raw = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$fp = fopen( $raw, 'r+');
$messg = $dbc . $usr . " : " . $msg . "<br>";
fwrite($fp,$messg);
fclose ($fp);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
unset($_POST); // remove all post values
header("Location: yourpage.php"); // your refresh header
}
?>
<html>
<head>
<title>Sm Chat</title>
<link rel="stylesheet" type="text/css" href="stylechat.css">
</head>
<body vlink='white' alink='white' link='white'>
<div class="nazi">
<a style="text-decoration:none" href="index.html"> Home </a>
</div>
<center>
<div class="cont">
<?php
echo "<br>" . $dbc;
?>
</div>
</center>
<center>
<form action="chat.php" method="post">
<div class="input-form">
<input type="text" value="" id="msg" name="msg" placeholder="Enter Your Message"/>
</div>
<input type="submit" value="Send" name="btn" class="btn"/>
</form>
</center>
</body>
</html>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
there are three diffrent files all in php please help mysql doen't returns a value from the database ?
//login.php
<?php
session_start();
include("includes/html_codes.php");
if (isset($_POST["email"])&&isset($_POST["password"])) {
$username = mysql_real_escape_string($_POST["email"]);
$password = mysql_real_escape_string($_POST["password"]);
if (!empty($username)&&!empty($password)) {
$query = mysql_query('SELECT * FROM users WHERE email=("$username") AND password=("$password")');
$count = mysql_num_rows($query);
$row = MySQL_fetch_row($query);
if($row==1){
echo 'ok';}
else {
echo 'Wrong Username or Password';
}
} else {
echo 'You must provide a username and password.';
} }
?>
##i guess the query is incorrect please help new to php##
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="en">
<head>
<title>Log in</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/main.css"/>
<link rel="stylesheet" href="css/form.css"/>
<link rel="stylesheet" href="css/register.css"/>
</head>
<body style="background-color:#EEEEEE">
<header>
<?php topBarm(); ?>//some function which works perfectly
</header>
<div id="wrapperLogin">
<center>
<form id="generalForm" class="container" action="<?php echo $current_file; ?>" method="POST">
<div class="field">Username: <input type="email" name="email" id ="email" class="input1"> </div> <div class="field">Password: <input type="password" name="password" id="password" class="input1"></div></center>
<center><div class="field"><input type="submit" class="buton" value="Log in" ></div>
</form></center></html>
""<please read the index.php file also it contains some important stuff>"
index file is including all the files so i didn't need to include it in login.php
//index.php
<?php
require 'includes/core.php';
require 'includes/connect.php';
include("login.php");
echo $current_file;
?>
##connection is established with this file##
>this code works perfectly
//connect.php
<?php
$conn_error = 'Could not connect.';
$mysql_host = 'localhost';
$mysql_user = 'my_user';
$mysql_pass = '';
$mysql_db = 'my_database';
if(!#mysql_connect($mysql_host, $mysql_user, $mysql_pass)||!#mysql_select_db($mysql_db)) {
die(mysql_error());
}
echo 'Connected!'
?>
Your query is badly formed. Try
$query = mysql_query("SELECT * FROM users WHERE email='" . $username . "' AND password='" . $password . "');"
Also, note that the mysql_ functions are deprecated. Update your code to mysqli or PDO.
First of all why do you have echos in the connection.php and why are you including so many files?
To learn basic debugging, you can do something like this with the result you are getting on your queries:
<pre>
<?php echo print_r($result) ?>
</pre>
Assuming you are doing a mysql_fetch_assoc or something similar with the result of the MySQL query
the variable enclosed in single quotes will not be displayed
$query = mysql_query("SELECT * FROM users WHERE email='".$username."' AND password='".$password."'");
I would like to change the upload directory depending on a hidden value in form submission.
As you can see I have a POST after move_uploaded_file that is submitted on the form but it fails to upload if I add the directory location. The directory has been created to the POST value prior to this. Thank you
<?php
session_start();
if($_SESSION['myusername'] == 'admin')
{
$header = 'headeradmin.php';
}
elseif (!empty($_SESSION['myusername']))
{
$header = 'header.php';
}
else
{header("location:../../login");}
require_once '../../connect.php';
$loggedin= $_SESSION['myusername'];
$result = mysql_query("SELECT * FROM login");
if (!$result) { // add this check.
die('Invalid query: ' . mysql_error());
}
$id = $_GET['id'];
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image upload</title>
<link href='http://fonts.googleapis.com/css?family=Boogaloo' rel='stylesheet' type='text/css'>
<link rel="StyleSheet" href="../../custom.css" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="js/multiupload.js"></script>
<script type="text/javascript">
var config = {
support : "image/jpg,image/png,image/bmp,image/jpeg,image/gif", // Valid file formats
form: "demoFiler", // Form ID
dragArea: "dragAndDropFiles", // Upload Area ID
uploadUrl: "upload.php" // Server side upload url
}
$(document).ready(function(){
initMultiUploader(config);
});
</script>
<link href="css/style.css" type="text/css" rel="stylesheet" />
</head>
<body lang="en">
<div id="content">
<?php require_once $header;
$sql_query = mysql_query("SELECT * FROM clients WHERE `id` = $id");
while($row = mysql_fetch_array($sql_query))
{
$username = $row['firstname'] . ' ' . $row['lastname'];
if (!file_exists('uploads/' . $username )) {
mkdir('uploads/' . $username, 0777, true);
}
}
?>
<h1 class="title">Multiple Drag and Drop File Upload</h1>
<div id="dragAndDropFiles" class="uploadArea">
<h1>Drop Images Here</h1>
</div>
<form name="demoFiler" id="demoFiler" enctype="multipart/form-data">
<input type="file" name="multiUpload" id="multiUpload" multiple />
<input type="hidden" value="<?php echo $username; ?>" name="username" id="username"/>
<input type="submit" name="submitHandler" id="submitHandler" value="Upload" class="buttonUpload" />
</form>
<div class="progressBar">
<div class="status"></div></div>
</div>
</body>
</html>
And this is where the form is redirected, I have now removed the echo but it does not seem to work.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_POST['username'] . '/' .$_FILES['file']['name'])){
echo($_POST['index']);
}
exit;
}
?>
First of all this code is that this code is not very secure. User can write there anything.
Second don't use echo in functions. Just $_POST['username'].
You need to remove echo
if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_POST['username'] . '/' .$_FILES['file']['name'])){
I would also suggest not using $_POST['username'] but instead use $_SESSION['username'] unless I am missing something and you are allowing users to upload files into other users directories
I want to save an unordered list to a column in a database. How can i do this ?
CHESSE
HAM
CREATE TABLE INGREDIENTE ( name varchar(20), ingredients varchar(30);
I want to put the list in the ingredients part.
Here is my code in the html and js i use to create the list.
function selectIngredient(select)
{
var $ul = $(select).closest('.ui-select').prev('ul');
console.log($ul[0])
if ($ul.find('input[value=' + $(select).val() + ']').length == 0) {
console.log('s')
$ul.append('<li onclick="$(this).remove();">' +
'<input type="hidden" name="ingredients[]" value="' +
$(select).val() + '" /> ' +
$(select).find('option:selected').text() + '</li>');
}
}
<!DOCTYPE html>
<html>
<head>
<title>Ingredient</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="themes/receta.min.css" />
<link rel="stylesheet" href="themes/receta.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
<script src="recetas.js"> </script>
</head>
<body>
<form action="insert.php" method="post">
Receta: <input type="text" name="receta">
Ingredientes: <input type="text" name="ingredientes">
<input type="submit">
<label>Ingredientes</label>
<ul>
</ul>
<select onchange="selectIngredient(this);">
<option value="Cheese">Cheese</option>
<option value="Olives">Olives</option>
<option value="Pepperoni">Pepperoni</option>
<option value="Milk">Milk</option>
</select>
</body>
</html>
<?php
$dbhost = 'localhost';
$dbuser = 'alanis_lozano';
$dbpass = '20Anahuac12';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'alanis_recetas';
mysql_select_db($dbname, $conn);
$sql="INSERT INTO Recetas (receta, ingredientes)
VALUES
('$_POST[receta]',$ingredientes = join(',', $_POST['selections'])";
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($conn);
?>
What you really want to do here is store ingredients in a delimited fashion, and format them into a list on output.
Try something like this:
<form action="save.php">
<input type="text" name="name" />
<textarea name="ingredients"></textarea>
<button type="submit">Save</button>
</form>
save.php - Replace any delimiter character with a newline (\n) character before saving
<?php
$name = $_GET["name"];
$name = mysqli_real_escape_string($name);
$ingredients = $_GET["ingredients"];
$ingredients = mysqli_real_escape_string($ingredients);
$ingredients = preg_replace("/[,\.\n\t]+/", "\n", $ingredients);
display.php - Before displaying, split up your ingredients by the newline character and render them in a UL
echo "<ul>";
$ingredients = explode('\n", $ingredients);
for ($i = 0; $i < count($ingredients); $i++) {
echo "<li>{$ingredients[$i]}</li>";
}
echo "</ul>";
You could convert the list into a comma delimited string:
$ingredients = join(',', $_POST['selections']);
In the dom you can name the select box selections.
I'm building a jQuery Mobile site, using PHP to interact with a MySQL database to dynamically populate the dropdown. Here's the code that creates the first page (a simple selection list within a form, with a submit button):
app.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<style>
/* App custom styles */
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js">
</script>
</head>
<body bgcolor="#000000">
<div data-role="page" data-theme="a" id="page1">
<div data-theme="a" data-role="header">
<h3>
Header
</h3>
</div>
<div data-role="content">
<div data-role="fieldcontain">
<form action="showView.php" method="POST">
<label for="selectmenu1">
Select a show:
</label>
<select name="selectmenu1" id="select_a_show">
<?php
$con = mysql_connect('tfis.db.7386546.hostedresource.com','tfis','Kurdt4794');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('tfis', $con);
$queryStr = 'SELECT * FROM Shows';
$result = mysql_query($queryStr);
while($row = mysql_fetch_array($result))
{
$episode_num = $row['episode_num'];
echo "<option value=" . $episode_num . ">Episode " . $episode_num . "</option>";
}
mysql_close($con);
?>
</select>
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
</body>
I then have a second page to handle the selected value, which I utilize in a PHP query to the database in the following code:
showView.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js">
</script>
</head>
<body>
<?php
$con = mysql_connect('tfis.db.7386546.hostedresource.com','tfis','Kurdt4794');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('tfis', $con);
$episode = $_REQUEST["select_a_show"];
$queryStr = "SELECT * FROM Shows WHERE episode_num = $episode LIMIT 1";
echo "Episode" . $episode . $queryStr;
$result = mysql_query($queryStr);
while($row = mysql_fetch_array($result))
{
$episode_title = $row['episode_title'];
$description = $row['description'];
$filename = $row['filename'];
$image_filename = $row['image_filename'];
$youtube = $row['youtube'];
}
echo "<div data-theme='a' data-role='header'> <h3> " . $episode_title . "</h3>
</div>
<div data-role='content'>
<img src='http://foxtechnyc.com/TFIS/images/" . $image_filename . "' />
</div>
<div>" . $description . "
<a data-role='button' data-transition='fade' href='http://foxtechnyc.com/TFIS/audio/" . $filename . "'>
Listen To Show</a>
<a data-role='button' data-transition='fade' href='http://www.youtube.com/watch?v=" . $youtube . "&feature=player_embedded>
Watch YouTube Video
</a></div>";
?>
<script>
alert(selection);
</script>
</body>
The issue is that the value is not being fetched correctly using the following code:
$episode = $_REQUEST["select_a_show"];
I read up on AJAX long polling from the server, since JQuery is client-side and PHP is server-side but I am unsure how I could implement it here.
Thanks in advance for the help!
In your form, the name of your select field should be name="select_a_show"
In other words, change line 30 in app.php from
<select name="selectmenu1" id="select_a_show">
to
<select name="select_a_show" id="select_a_show">
That should do the trick.