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
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.
I am very new to PHP and started making a light weight CMS. I have stored all the body content in the database and the CMS calls it from the database and displays it in a text area to be edited. However I was wondering is there a way to make it display the text without HTML tags. I have tried the strip_tag function however when I hit save on my cms it saves without the html tags! how will I go about making it display the data from the database without HTML tags but when I save it, it will save with the HTML tags! Sorry if this question is not clear but it is quite difficult to explain. Here is my code so far working fine:
<?php include_once "includes/scripts.php"; ?>
<?php include_once "includes/connect.php";?>
<?php include_once "includes/cms_page_security.php";?>
<?php
$sql = "SELECT * FROM content WHERE id = '5'";
$result = mysql_query($sql, $connect);
$num= mysql_numrows($result);mysql_close();
$row = mysql_fetch_row($result);
$pg_content = $row['1'];
if (isset($_POST['saveChanges'])){
$pgcontent = $_POST['edit'];
$sql_query = ("UPDATE content SET cage_content= '$pgcontent' WHERE cage_content= '$pg_content'");
mysql_query($sql_query,$connect);
header('location: admin_cms_staff.php');
$feedback = "Updated successfully";
}
?>
<div id="cms_container"><br>
<h1>Staff Page<img src="images/three_column_grid_line.png" alt="line"></h1>
<form id="form1" name="form1" method="post">
<textarea id="content" name="edit"><?php echo $pg_content; ?></textarea>
<input type="submit" class="submit_edit" value="Save" name="saveChanges" onClick="alertFunction()">
</form>
<p class="logout_btn">Back</p>
<?php if(isset($_POST['saveChanges'])){
echo $feedback;}?>
</div><!--cms_container-->
<script>
function alertFunction()
{
var r=confirm("Do you want to save the changes you made to the page?");
if (r==true)
{
}
else
{
return;
}
}
</script>
</body>
</html>
Change this:
$pgcontent = $_POST['edit'];
to:
$pgcontent = strip_tags($_POST['edit']);
And also change this:
<textarea id="content" name="edit"><?php echo $pg_content; ?></textarea>
to:
<textarea id="content" name="edit"><?php echo strip_tags($pg_content); ?></textarea>
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; ?>">
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
I am trying to get a guest book to work using PHP. I have managed to make it function, the thing is that I don't want the guest book to be in my index.php. I want it to be on a dynamic page, index.php?=guestbook for instance.
The problem is that when I put the code on another page rather than index.php the thing that happends when I fill out the fields and press the submit button, I get redirected to index.php and nothing is submited to my database. This all works fine as long as the code is in the index.php.
My first question is: What is causing this?
Second question: How do I get the code to function properly eventhough I have it in index.php?=guestbook?
Thanks in advance!
I am using xampp btw.
See below for the code:
<html>
<head>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
</head>
<body>
<h1>Guestbook</h1><hr>
<?php
mysql_select_db ("guestbookdatabase") or die ("Couldn't find database!");
$queryget = mysql_query ("SELECT * FROM guestbook ORDER BY id ASC") or die("Error witch query.");
$querygetrownum = mysql_num_rows ($queryget);
if ($querygetrownum == 0)
echo "No posts have been made yet. Be the first!";
while ($row = mysql_fetch_assoc ($queryget))
{
$id = $row ['id'];
$name = $row ['name'];
$email = $row ['email'];
$message = $row ['message'];
$date = $row ['date'];
$time = $row ['time'];
if ($id%2)
$guestbookcomment = "guestbookcomment";
else
$guestbookcomment = "guestbookcommentdark";
echo "
<div class='$guestbookcomment'>
<div class='postheader'>
<b>Posted by $name ($email) on $date at $time</b>
</div>
<div class='message'>
".nl2br(strip_tags($message))."
</div>
</div>
";}
echo "<hr>";
if($_POST['submit'])
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$date = date("Y-m-d");
$time = date("H:i:s");
if ($name&&$email&&$message)
{
$querypost = mysql_query ("INSERT INTO guestbook VALUES ('','$name','$email','$message','$date','$time')");
echo "Please wait... <meta http-equiv='refresh' content='2'>";
}
else
echo "Please fill out all fields.";
}
echo "
<form action='index.php' method='POST'>
Your name: <input type='text' name='name' class='name' maxlength='25' ><br> <br>
Your email: <input type='text' name='email' class='email' maxlength='35'><br><br>
<div class='your_message'>
Your message:<input type='textarea' name='message' class='messagetextarea' maxlength='250'><br><br>
</div>
<input type='submit' name='submit' value='Post'>
</form>
";
?>
</body>
</html>
1) The action property of your form should be the same as the name of the file where the code is in. :) You create a guestbook.php, for example, but the action still is 'index.php'. Hence the problem. You send the POST data to index.php but there's no code to process it.
2) The query string doesn't affect the form. Only the filename.
I hope I understood your problem correctly.
Have you tried updating your form's action parameter to:
index.php?=guestbook
instead of just index.php?
If the problem resides on the server end than the victim to your problem is .htaccess (mod rewrite);
Otherwise, what do you really mean by this line of code?
echo "Please wait... <meta http-equiv='refresh' content='2'>";
< meta > refresh tag requires location to be mentioned where the redirect otherwise according to you refreshes the current page..
<meta http-equiv="refresh" content="2;url=http://stackoverflow.com/">
First, I'm assuming the file you're showing is index.php
Second, don't use index.php?=guestbook. URL parameters work within a key => value structure. In you're case you've only defined the value and no key.
Try using index.php?page=guestbook. this way, in your index.php file you can do something like:
if($_GET['page'] == 'guestbook') {
// ... your guestbook php code.
}
Then try setting your forms action attribute like this: action="index.php?page=guestbook".
Third, I'm going to assume that you have mysql connection code that isn't shown here. If not, take a look at mysql_connect().
Fourth, NEVER use unescaped data in a SQL query. You MUST escape your data to protect your database from being destroyed. Take a look at this wikipedia article which describes SQL Injection in greater detail: http://en.wikipedia.org/wiki/SQL_injection
Then take a look at mysql_real_escape_string() to learn how to prevent it with PHP and MySQL.
Fifth, don't use <meta http-equiv='refresh' content='2'> for redirect. Use PHP's header() function to redirect users, like this:
header('location: index.php');
exit(); // be sure to call exit() after you call header()
Also, just so you know, you CAN close PHP tags for large HTML blocks rather than using echo to print large static chunks of HTML:
<?php
// ... a bunch of PHP
?>
<form action="index.php" method="POST">
Your name: <input type="text" name="name" class="name" maxlength="25" ><br> <br>
Your email: <input type="text" name="email" class="email" maxlength="35"><br><br>
<div class="your_message">
Your message:<input type="textarea" name="message" class="messagetextarea" maxlength="250"><br><br>
</div>
<input type="submit" name="submit" value="Post">
</form>
<?php
// ... some more PHP
?>