Im having trouble passing the value id in my code to edit.php.
In displaynews i print out articles from the database. It also creates a link that redirects me to edit.php and sending the $id value with it.
Link to displaynews function
http://snipt.org/zhla8
Here's where im having trouble
<h3>EDIT NEWS ARTICLE</h3>
<form id="EditNews" name="EditNews" method="POST" action="edit.php">
<textarea name="editnewstext"><?php $news=Textarea(1);echo $news ?></textarea> <!--HERE i need to replace the 1 with id passing in displaynews -->
<input type="submit" name="Edit_News" id="Edit_News">
<?php
include 'db.php';
include'editnewsarticle.php';
if(isset($_POST['Edit_News']))
{
$content= $_POST['editnewstext'];
geteditnews(1,$content); //<!--HERE i need to replace the 1 with idpassing in displaynews -->
Header("location:Home.php");
}
Link to Edit.php page
http://snipt.org/zhkj8
Link to GetnewsTextarea function
http://snipt.org/zhlb9
Link To editnewsarticle function
http://snipt.org/zhki2
Please dont comment on how the mysql extension is depreciated and that my code is open for sql injections.
Thanks in advance
EDIT: Here's the solution
if(isset($_GET['id']))
{
$id = $_GET['id'];
$data = mysql_query("SELECT * FROM news WHERE id = '$id' ");
}
?>
Does these changes in edit.php help?
if (isset($_POST['id']))
$id = $_POST['id'];
<?php $news=Textarea($id);echo $news ?>
geteditnews($id, $content);
Add a hidden field for the id right after the form tag. As such:
<form id="EditNews" name="EditNews" method="POST" action="edit.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">
Related
I am currently trying to make a basic website with php in which I can essentially edit blogs etc.. and when i9 double click the current text I have made it so that an iframe popup opens for text to be changed. This iframe has a src of editbox.php?id=<?php echo $id ?>($id works for the parent page).
The problem is that the $Id in the code doesnrt seem to get recognised in the $query part even though it seems to have teh correct value
The code for the iframe page is below:
<?php
include_once('../includes/connection.php');
include_once('../includes/article.php');
$article = new Article;
$id =$_GET['id'];
print_r($id);
$data = $article->fetch_data($id);
if (isset($_POST['content'])) {
$content = $_POST['content'];
$uid =$_GET['id'];
$query = $pdo->prepare("UPDATE articles SET article_content='$content' WHERE article_id='$id'");
$query->execute();?>
<script>parent.location.href=parent.location.href</script>
<?php }?>
<script>
function textAreaAdjust(element){
element.style.height = "auto"
element.style.height = (25+element.scrollHeight)+"px";
}</script>
<link rel="stylesheet" href="../Assets/style2.css" />
<form action="editbox.php" method="post" autocomplete="off">
<textarea autofocus="true" onfocus="textAreaAdjust(this);" onkeyup="textAreaAdjust(this)" cols="157" placeholder="Content" name="content" ><?php echo $data['article_content']?></textarea><br /><br />
<input type="Submit" value="Add article" />
</form>
What's strange is that the print_r returns a 3 which is what its supposed to return but for some reason it doesn't work.
Also it does work if I change it from $id =$_GET('id') to $id=3 but since id is dynamic I cant do this.
Sorry if the code above has other problems, I'm a newbie.
Am creating a webpage which allows me to search for locations by county. I am trying to create an href link on my basic homepage which will call to a Php function and then display results from my Sql database.
Not sure why but when I click on the href link the homepage just goes blank, no values returned. Have been over code and database, have shown to a few friends and can't figure out what's wrong. Any help appreciated!
This is my index.php main homepage code (with href about halfway down)
<!DOCTYPE HTML>
<?php
include("includes/db.php");
include("getAntrim.php");
include("functions/functions.php");
?>
<html>
<head>
<link rel = "stylesheet" href="styles/styles.css" media = "all"/>
<title>Location Scout</title>
</head>
<body>
<!-- Main container starts -->
<div class ="main_wrapper">
<div id="form">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="user_query" placeholder="Search for location"?>
<input type="submit" name="search" value="search"/>
</form>
</div>
<div class ="content_wrapper">
<div id ="left_sidebar">
<div id="sidebar_title">Search by County</div>
Antrim<br></br>
<div id ="content_area">
<div id ="products_box">
<!-- THIS IS WHERE FETCHED DATABASE INFO WILL GO -->
<?php
getAntrim();
?>
</div>
</div>
</div>
<!-- Main container ENDS -->
</div>
</div>
</body>
</html>
This is my getAntrim.php function, which should sort through sql database and then return stated values.
<?php
include ("includes/db.php");
if (isset ($_GET['getAntrim'])) {
$get_loc_co = "select * from locations where county='Antrim'";
$run_loc_co = mysqli_query($db, $get_loc_co); //Gets data from db and presents on main page
$count = mysqli_num_rows($run_loc_co);
if ($count==0) {
echo "<h2>No locations found in this category.";
}//if
while ($row_loc_co=mysqli_fetch_array($run_loc_co)) {
//variable to store values
$loc_id = $row_locations['loc_id'];
$loc_name = $row_locations['loc_name'];
$town = $row_locations['town'];
$county = $row_locations['county'];
$productions = $row_locations['productions'];
$disabled = $row_locations['dis_access'];
$parking = $row_locations['parking'];
$visitor = $row_locations['vis_facs'];
$transport = $row_locations['public_trans'];
$cost = $row_locations['cost'];
$accom = $row_locations['accom'];
$latitude = $row_locations['latitude'];
$longitude = $row_locations['longitude'];
$description = $row_locations['loc_desc'];
$keyword = $row_locations['loc_keyword'];
$loc_image = $row_locations['loc_img'];
echo "
<div id= 'single_location'>
<h3>$loc_name</h3>
<img src = 'Admin_area/location_images/$loc_image' width='180' height = '180'/><br>
<p><b>Productions: $productions </b></p>
<p><b>Description: $description </b></p>
</div>
";
}//while
}//if
?>
Am first time poster, so hopefully have posted this ok! Any advice appreciated.
you're checking for a query parameter, which you're NOT passing in:
Antrim<br></br>
if (isset ($_GET['getAntrim'])) {
^^^^^^^^^
$_GET contains url query parameters, e.g. example.com?foo=bar (the foo=bar portion). Since you have NO query parameters in your href, the isset() properly returns false, and your entire db code section is simply ignored.
You probably want
Antrim<br></br>
^^^^^^^^^^
to make this work as-is.
to be honest this is more of a how to then help with code i already have. So i hope this is okay, else of course i will delete my question again. Anyway here goes i have a site with boxes, with a picture headline and a submit button. All the info in these boxes is being delivered, from my database. And of course in my database i also have a id cell, and if i try to echo out the id cell with the rest of the info in the box it shows up fine. But when i try to assign the id output variable to a header location, i do for some weird reason always get the id 3. Eventhough the id´s shows up perfectly fine, in the boxes. I have included my php code and i am still a beginner to php so sorry for this noob question. :)
session_start();
include 'connection.php';
$sqlSelect = mysqli_query($con,"SELECT * FROM inspi");
while ($feed=mysqli_fetch_array($sqlSelect))
{
$id = $feed['id'];
if(isset($_POST['readArticle']))
{
$id = $_SESSION['id'];
header("Location:"."redirect.php?".SID.$idArticle);
}
?>
<div class="contentBoxOne">
<img width="100%" height="170px" src="userpics/<?php echo $feed['image']; ?>">
<div class="line"></div>
<form method="post" action="">
<input type="submit" name="readArticle" class="readArticle" value="Læs nu!">
</form>
<?php $idArticle= $feed['id'];?>
<h2><?php echo $feed['headline'];?></h2>
</div>
You are setting $idArticle at the bottom of the loop but trying to use it at the top so it will be pulling it from the previous result. Try:
while ($feed=mysqli_fetch_assoc($sqlSelect)){
$idArticle= $feed['id'];
$sid = $_SESSION['id'];
if(isset($_POST['readArticle']))
{
header("Location:"."redirect.php?".$sid.$idArticle);
}
//rest of code
}
You will have to put div inside the loop.
I also replaced the header redirect with form action attribute (you may want to replace method POST with GET instead).
ID is passed with a hidden field
<?php
include 'connection.php';
$sqlSelect = mysqli_query($con,"SELECT * FROM inspi");
while ($feed=mysqli_fetch_assoc($sqlSelect))
{
$id = (int)$feed['id'];
?>
<div class="contentBoxOne">
<img width="100%" height="170px" src="userpics/<?php echo $feed['image']; ?>">
<div class="line"></div>
<form method="post" action="redirect.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="submit" name="readArticle" class="readArticle" value="Læs nu!">
</form>
<h2><?php echo $feed['headline']; ?></h2>
debug: <pre><?php print_r($feed); ?></pre>
</div>
<?php } // end of while loop ?>
I'm having a very annoying issue and I have no idea what I'm doing wrong. It has (again) something to do with loading in a page into a DIV.
I have made a form which can be used to update information into a database. This form and PHP code is in one file and is being loaded in one DIV. When I visit the page which is loaded into the DIV itself, everything is working fine and the database is being updated as it should.
Though, when the page is loaded into the page. When I press submit nothing happens. What I want to happen is that the echo, which is either "Success!" or "Error!" is being displayed in the same DIV as the page is loaded into. This is my code, I hope someone can help! some variables are in Dutch, excuse me for that.
if(isset($_POST['submit'])) {
include "database.php";
session_start();
$id = $_POST['id'];
$titel = $_POST['titel'];
$text = $_POST['text'];
$categorie = $_POST['categorie'];
$auteur = $_SESSION['sess_loginnaam'];
$laatst_aangepast = date("Y-m-d H:i:s");
$sql="UPDATE paginas SET id='$id', titel='$titel', text='$text', categorie='$categorie', auteur='$auteur', laatst_aangepast='$laatst_aangepast' WHERE id='$id'";
$result=mysql_query($sql);
if($result){
echo "Success!";
?>
<META HTTP-EQUIV="refresh" content="2;URL=index.php">
<?php }
else {
echo "Mislukt!";
}
}
else {
include "database.php";
$id = $_GET['id'];
$sql="SELECT * FROM paginas WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
<form name="form1" method="post" action="">
ID:
<input name="id" type="text" id="id" value="<? echo $rows['ID']; ?>" size="2"></div> Titel:
<input name="titel" type="text" id="titel" value="<? echo $rows['titel']; ?>" size="50%"> Categorie: <select name="categorie"><option value="Paginas">Pagina's</option>
</select>
Tekst:
<textarea name="text" type="text" id="text" rows="31" cols="79"><? echo $rows['text']; ? ></textarea>
<button type="submit" name="submit">Edit!</button>
</form>
And here is the code I use to load this page into the DIV:
$("#edit").on('click',function(){
$('#content').load($(this).attr('href'));
});
So how can I manage to display the echo into the same DIV? :)
$.load is equivalent to $.get whereas your php code detect $_POST, that's way there is no response.
You can change your js code to
$("#edit").on('click',function(){
$.post($(this).attr('href'), YOUR_FORM_DATA_HERE, function(data){
//update your page here with response
});
});
See $.load - jQuery Doc
I've searched high and low both on Google and this site for an answer to this question with no luck.
I'm working on a custom CMS for future customers who want to do their own maintenance. Here's the code that loads the current page content into the textarea from the database:
if(isset($_GET['id'])) {
$id = $_GET['id'];
$content = mysql_query("SELECT title, content FROM pages WHERE id=".$id);
$search = mysql_num_rows($content);
if($search > 0){
while($page = mysql_fetch_array($content)) { ?>
<h1>Editing <?php echo $page['title']; ?></h1>
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<textarea id="editor" name="content"><?php echo $page['content']; ?></textarea>
</form>
<?php }
}
} ?>
Here's the code in save.php:
<?php
if(isset($_POST['content'])){
$content = $_POST['content'];
$id = $_GET['id'];
echo $content;
mysql_query("UPDATE pages SET content=".$content." WHERE id=".$id);
}
?>
The problem is that the POST['content'] keeps getting the original content, not the edited one that the user just submitted.
How do I fix this?
The data should be passed automatically when the form is posted. Here is the "Integration" page page from the Developers Guide that explains this:
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Integration
Are you sure that the query is being run successfully and that the content field is being updated? I ask because the code you posted says that in save.php, you are using:
$id = $_GET['id'];
This would seem to cause a problem, because you are using method="post" in your form, not method="get".
Could you also post the code you are using to replace the textarea with CKEditor. Are you using the PHP method:
<?php
$CKEditor = new CKEditor();
$CKEditor->basePath = '/ckeditor/';
$CKEditor->replace("content");
?>
Or the JavaScript method:
<script language="Javascript">
<!--
CKEDITOR.replace( 'content' );
//-->
</script>
Be Well,
Joe
Follow up to question in comments
Hi Purmou,
I didn't notice that you were including the id in the form action, sorry about that. If you did want to use it as a $_POST variable instead, you could include it as a hidden field like this:
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<input type="hidden" id="id" name="id" value="<?php echo $_GET['id']; ?>" />
<textarea id="editor" name="content"><?php echo $page['content']; ?></textarea>
There is some good documentaion about loading the editor via PHP in the _samples folder of the CKEditor install:
http://YourSite.com/ckeditor/_samples/php/
http://YourSite.com/ckeditor/_samples/php/replace.php, has the basic settings:
<?php
// Include the CKEditor class.
include_once "ckeditor/ckeditor.php";
// Create a class instance.
$CKEditor = new CKEditor();
// Path to the CKEditor directory.
$CKEditor->basePath = '/ckeditor/';
// Replace a textarea element with an id (or name) of "textarea_id".
$CKEditor->replace("textarea_id");
?>
Similar to the JavaScript method, you can add config options before you replace the textarea. An example from the "advanced.php" file:
$CKEditor->config['width'] = 600;
To use the PHP method with your specific code, do this:
if(isset($_GET['id'])) {
$id = $_GET['id'];
$content = mysql_query("SELECT title, content FROM pages WHERE id=".$id);
$search = mysql_num_rows($content);
if($search > 0){
while($page = mysql_fetch_array($content)) { ?>
<h1>Editing <?php echo $page['title']; ?></h1>
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<textarea id="content" name="content"><?php echo $page['content']; ?></textarea>
</form>
<?php }
include_once "ckeditor/ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = '/ckeditor/';
$CKEditor->replace("content");
}
} ?>
I changed the textarea id from "editor" to "content". I would recommend against using "editor" for the id or name, because it's used in the CKEditor code to refer to the CKEditor instance.
You can do config settings in the page where you load the editor or in the config.js file or in your own custom config file.
I spent some time trying to catch the value of the form content field after the form is submitted, but was only able to see it before CKEditor had updated the contents.
Be Well,
Joe