i have a cms which creates pages in the form of index.php?p=PAGENAME
for example:
index.php?p=01Home
index.php?p=news
index.php?p=about
or whatever pagename
now what i want to do is to get the PAGENAME as a variable
for example
$page="01home";
$page="news";
$page="about";
i tried to get the pagename via $_GET[p] from my cms code, but that doesnt work, any other ideas? The code must be independent from the cms so i can use the pagename in a plugin
PLUGIN code where i want o use the pagename
<?php
$amount= $cfr ;
$page = $_GET['p'];
if (!isset($_GET["action"])) {
}
elseif ($_GET["action"] == "write") // etc.
{
header('location: succes.php');
$data = ''.PHP_EOL;
$file=fopen("plugins/comments/comments/$page/comments.txt",'a');
$message=str_replace("\r",'',$message);
$message=str_replace("\n",'{{',$_GET['message']);
fwrite($file,$_GET['name'].'||'.date('d-m-Y').'||'.$message.$data);
fclose($file);
}
$start=(isset($_GET['start'])?$_GET['start']:0);
$gastenboek=Array();
$gastenboek=file("plugins/comments/comments/$page/comments.txt");
?>
<br>
<fieldset class="comments">
<legend class="pm">Reacties</legend>
<div style="float:right;">Aantal reacties: <b><?php echo min($start+$amount,sizeof($gastenboek)); ?></b></div>
<TABLE class="comments" width="100%">
<?PHP
$gastenboek=array_reverse($gastenboek);
for ($i=$start;$i<$start+$amount && $i<sizeof($gastenboek);$i++) {
list($name,$date,$message)=explode('||',$gastenboek[$i]);
$message=str_replace('{{',"\n",$message);
echo '<TR><TD class="comments"><div style="float:left;">
<B>'.$name.'</B></div><div style="float:right;">
(<i>'.$date.'</i>)</div><br><p>'.str_replace("\n",'<BR>',htmlspecialchars($message)).'</p> </TD></TR>'."\n";
}
?>
</TABLE>
</fieldset>
<br>
<fieldset class="pm">
<legend class="pm">Plaats een reactie</legend>
<FORM action="index.php" method="GET" onSubmit="return validate(this);" name="comments" id="comments">
<INPUT type="hidden" name="action" value="write">
<p class="pm">Naam: </p>
<INPUT type="text" name="name" size="30" style="width:300px;" required><br>
<p class="pm2">Reactie: </p>
<TEXTAREA class="message" name="message" id="message" cols="46" rows="5" required></TEXTAREA><br>
<INPUT type="submit" value="Reactie toevoegen">
</FORM>
</fieldset>
<script type="text/javascript">
$(function(){
$('#comments').ebcaptchaword();
});
</script>
The url being - index.php?p=01Home
Try this -
$page = $_GET['p'];
If it says ?p=pagename that should mean you should use $_GET['p']... or I'm not getting this problem right.
Related
Here is my php code:
<?php
try{
//Need to make a connection
$con=new PDO("mysql:host=localhost;dbname=courseraassignment","root","");
$statementUpdate=$con->prepare("select * from position
where profile_id=:profile_id");
$statementUpdate->execute(array(
':profile_id'=>$_REQUEST['profile_id']
));
foreach($statementUpdate as $row){
echo $row['year'];
echo $row['description'];
echo "<br>";
}
}catch(PDOException $e){
echo "error".$e->getMessage();
}
?>
From this PHP code, I wanted to achieve data including the year and description of the position table from the database.
Here is my complete html code:
<div class="container">
<h1>Ashiful Islam Prince's Resume Registry</h1>
<h1>Editing Profile for UMSI</h1>
<?php
if(isset($_SESSION['profile_error'])) {
$error = $_SESSION['profile_error'];
unset($_SESSION["profile_error"]);
echo '<span class="text-danger">';
echo $error;
echo '</span>';
}
?>
<form method="post">
<p>First Name:
<input type="text" name="first_name" value="<?php
echo $firstName;
?>"
></p><br>
<p>Last Name:
<input type="text" name="last_name" value="<?php
echo $lastName;
?>"</p>
<p>Email:
<input type="text" name="email" value="<?php
echo $Email;
?>"</p><br>
<span class="text-danger">
<?php
if(isset($email_sign_error))
echo $email_sign_error;
?>
</span>
<p>Headline:<br/>
<input type="text" name="headline" value="<?php
echo $Headline;
?>"</p>
<p>Summary:<br/>
<textarea name="summary" rows="8" cols="80">
<?php
echo $Summary;
?>
</textarea>
<p>Position : <input type="button" class="add_position" id="add_position"
name="addPosition" value="+">
<div class="position_fields" id="position_fields">
</div>
<p>
<input type="submit" value="Save" name="btn" class="btn btn-primary">
<input type="submit" name="cancel" value="Cancel">
</p>
</form>
</div>
</div>
</div>
Here is the part of code from above:
<div class="position_fields" id="position_fields">
</div>
In this part, I want to show these fetched values(The year and the description).
Here is my JQUERY code:
<script type="text/javascript">
$(document).ready(function(){
var countPos=0;
var add_button=$('#add_position');
var wrapper=$('#position_fields');
window.console && console.log('Document ready called');
//When the user clicks on the add button then It inserts these input fields
function addField(event){
if ( countPos >=9 ) {
alert("Maximum of nine position entries exceeded");
return;
}
countPos++;
window.console && console.log("Adding position "+countPos);
$(wrapper).append(
'<div id="position'+countPos+'"> \
<p>Year: <input type="text" name="year'+countPos+'" value="" /> \
<input type="button" value="-" \
onclick="$(\'#position'+countPos+'\').remove();return false;"></p> \
<textarea name="desc'+countPos+'" rows="8" cols="80"></textarea>\
</div>');
}
$(add_button).click(function(event){
addField();
});
addField();
});
</script>
This code is developed for handling the dynamic input field issues. When the user clicks to the add button then dynamic input fields will be displayed and these fields will be shown at the time of page loads as well.
I want to achieve:
Everything will be done as same as it is. But when the page loads these dynamic input fields will be displayed with values. How can I achieve my goal?
I am trying to understand how forms work , so far I understood that I could submit the form and refresh to the same page through
> action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
and then catch the errors or not have blank inputs.
However, let's say I have anchors in the page (sections in other words like #ContactUs) how could I refresh the page using action to get to that specific place instead of going back to top of page?
Thanks for all in advance
Here is part of the code:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h2>SEND US A MESSAGE!</h2>
<span>We'd be happy to hear from you.</span>
<input name="contactname" placeholder="Name" type="text" value="<?php echo $contactname;?>"/> <span class="error"> <?php echo $contactnameErr;?></span>
<input name="contactemail" placeholder="Email" type="text" value="<?php echo $contactemail;?>" /><span class="error"> <?php echo $contactemailErr;?></span>
<input name="contactphone" placeholder="Phone #" type="text" value="<?php echo $contactphone;?>" /><span class="error"> <?php echo $contactphoneErr;?></span>
<input name="contactsubject" placeholder="Subject" type="text" value="<?php echo $contactsubject;?>" /><span class="error"> <?php echo $contactsubjectErr;?></span>
<textarea name="contactmessage" placeholder="Message"><?php echo $contactmessage;?></textarea><span class="error"> <?php echo $contactmessageErr;?></span>
<input type="submit" name="submit" value="Send" class="contact-button" />
</form>
You should have a validation for it for example
<?php
if(isset($_POST['submit'])){
//then do something
header("Location: #contactus");
}
?>
So this way even if you load, it does not give you empty and does not ask you to resubmit
You can Use jQuery as i given below
<?php
if(isset($_POST['submit']))
{
//
//sql query and display
//
?>
<script>
$(function() {
$('html, body').animate({
scrollTop: $("#contactus").offset().top
}, 2000);
});
</script>
<?php
}
?>
I am using php and CodeIgniter. I am a novice at both of these (career VB.Net and C# developer). However, trying to create a basic registration form, I'm having a hard time getting jQuery validation to work.
header.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE html>
<html>
<head>
<?php if(isset($title)) {?>
<title>Discuss Cards - <?php echo $title ?></title>
<?php } else { ?>
<title>Discuss Cards</title>
<?php } ?>
<link rel="stylesheet" href="<?php echo base_url();?>styles/forum.css" type="text/css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="<?php echo base_url();?>js/jquery.validate.min.js"></script>
</head>
<body>
<?php if(isset($title)) {?>
<h1><?php echo $title?></h1>
<?php } ?>
<?php if (isset($page_description)) {?>
<p id="page_description"><?php echo $page_description?></p>
<?php } ?>
create.php
<script>
$("#createaccount").validate();
</script>
<?php $attributes = array('id'=>'createaccount');
echo form_open('user/create_account',$attributes); ?>
<?php echo form_label('Email','txtEmail');?>
<br />
<?php $data = array('type'=>'email','name'=>'txtEmail','id'=>'txtEmail','maxlength'=>'50','minlength'=>'2');
echo form_input($data); ?>
<br /><br />
<?php echo form_label('Username','txtUsername');?>
<br />
<?php $data = array('name'=>'txtUsername','id'=>'txtUsername','maxlength'=>'50','minlength'=>'5');
echo form_input($data); ?>
<br /><br />
<?php echo form_label('Password','pswPassword');?>
<br />
<?php $data = array('name'=>'pswPassword','id'=>'pswPassword');
echo form_password($data); ?>
<br /><br />
<?php echo form_label('Confirm Password','pswConfirmPassword');?>
<br />
<?php $data = array('name'=>'pswConfirmPassword','id'=>'pswConfirmPassword');
echo form_password($data); ?>
<br /><br />
<input type="submit" value="Register" name="Register" />
<?php echo form_close();?>
footer.php
<strong>© 2016</strong>
</body>
</html>
result...
<!DOCTYPE html>
<html>
<head>
<title>Discuss Cards - Create New Account</title>
<link rel="stylesheet" href="http://[::1]/forum/styles/forum.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://[::1]/forum/js/jquery.validate.min.js"></script>
</head>
<body>
<h1>Create New Account</h1>
<script>
$("#createaccount").validate();
</script>
<form action="http://[::1]/forum/index.php/user/create_account" id="createaccount" method="post" accept-charset="utf-8">
<label for="txtEmail">Email</label>
<br>
<input type="email" name="txtEmail" value id="txtEmail" maxlength="50" minlength="2">
<br>
<br>
<label for="txtUsername"></label>
<br>
<input type="text" name="txtUsername" value id="txtUsername" maxlength="50" minlength="5">
<br>
<br>
<label for="pswPassword">Password</label>
<br>
<input type="password" name="pswPassword" value id="pswPassword">
<br>
<br>
<label for="pswConfirmPassword">Confirm Password</label>
<br>
<input type="password" name="pswConfirmPassword" value id="pswConfirmPassword">
<br>
<br>
<input type="submit" value="Register" name="Register">
</form>
<strong>© 2016</strong>
</body>
</html>
Now, the links to the js and css files are correct. I used the example from http://jqueryvalidation.org/documentation/:
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>Please provide your name, email address (won't be published) and a comment</legend>
<p>
<label for="cname">Name (required, at least 2 characters)</label>
<input id="cname" name="name" minlength="2" type="text" required>
</p>
<p>
<label for="cemail">E-Mail (required)</label>
<input id="cemail" type="email" name="email" required>
</p>
<p>
<label for="curl">URL (optional)</label>
<input id="curl" type="url" name="url">
</p>
<p>
<label for="ccomment">Your comment (required)</label>
<textarea id="ccomment" name="comment" required></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
<script>
$("#commentForm").validate();
</script>
However, I feel that I'm missing something because it's not working. When I use their demo (http://jqueryvalidation.org/files/demo/), a new label is created under the input area which displays the error. However, when I test my code (Notepadd++, latest version of Google Chrome, WAMPServer 2.5), I only get Chrome validation. Could someone point out to me what I'm doing incorrect? Thanks.
A bit too long for a comment, but not sure if it is the only problem. The first thing I see is that the
<script>
$("#createaccount").validate();
</script>
is located before the form with id="createaccount". This means this javascript code is parsed and run before the element exists inside the DOM.
In order to run this JS when the DOM is loaded, you then need to wrap it into this specific jquery part :
$( document ).ready( function( ) {
$("#createaccount").validate();
}
(And ideally you put all JS at the end of the page, before the closing </body> tag)
I have a update query that I want to use and it's not working. All data is being posted except for CommentID and I can't understand why.
This is my query's output:
UPDATE comments SET
title='PHP',universitet='Högskolan',
kurs='Objekt orienterad programmering i PHP',
kurskod='HIG480-34', betyg='8', message='kom igen nu PHP'
WHERE CommentID = ''
As you can see WHERE CommentID = '' is empty.
<?php
require_once 'DBConnection/connection.php';
class EditPost{
public $comment;
public $id;
public function __construct() {
$this->comment = comment;
$this->id = mysql_real_escape_string($_GET['CommentID']);
}
public function EditThePost(){
if(!isset($_POST['editComment'])){
$query = "SELECT * FROM comments WHERE CommentID = '$this->id'";
$result = mysql_query($query);
$this->comment = mysql_fetch_array($result);
}elseif(isset($_POST['CommentID'])){
$updateQuery = "UPDATE comments SET title='$_POST[title]',universitet='$_POST[universitet]',kurs='$_POST[kurs]',kurskod='$_POST[kurskod]',betyg='$_POST[betyg]',message='$_POST[TheComment]' WHERE CommentID = '$_POST['CommentID]'";
mysql_query($updateQuery) or die(mysql_error());
echo $updateQuery;
header("Location: loggedin.php");
exit();
}
}
}
Here is the edit page with HTML:
<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
require_once 'DBConnection/connection.php';
require_once 'Posting/editPost.php';
$edit = new EditPost();
$edit->EditThePost();
?>
<!DOCTYPE html>
<html lang="sv">
<?php include('incl/header.php'); ?>
<body>
<!--The Navbar-->
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container" align="center">
Hem ||
<?php include('incl/logoutUser.php'); ?>
</div>
</div>
<!--The page container-->
<div id="container" >
<img src="logo.png" id="logoType" align="center">
<br>
<br>
<span class="label label-warning">Redigera inlägg:</span>
<div class="container" align="left">
<br>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p><span class="label label-info">Titel: </span> <br><input type="text" require name="title" placeholder="Ange titel.." value="<?php echo $edit->comment['title'] ;?>"</p>
<p><span class="label label-info">Högskola: </span> <br><input type="text" require name="universitet" placeholder="Ange högskola.." value="<?php echo $edit->comment['universitet']?>"></p>
<p><span class="label label-info">Kurs: </span> <br><input type="text" require name="kurs" placeholder="Ange kurs.." value="<?php echo $edit->comment['kurs']; ?>"></p>
<p><span class="label label-info">Kurskod: </span> <br><input type="text" require name="kurskod" placeholder="Ange kurskod.." value="<?php echo $edit->comment['kurskod']; ?>"></p>
<p><span class="label label-info">Betyg: </span> <br><input type="text" require name="betyg" placeholder="Betyg mellan 1-10" value="<?php echo $edit->comment['betyg']; ?>"></p>
<p><span class="label label-info">Meddelande: </span></p>
<textarea rows="10" cols="80" require name="TheComment" placeholder="Skriv ditt meddelande.." ><?php echo $edit->comment['message'];?></textarea>
<br><br>
<input type="hidden" name="CommentID" value="<?php echo $_POST['CommentID'];?>"/>
<p><input type="submit" class="btn btn-primary" name="editComment" value="Redigera inlägg"></p>
<br>
</form>
<br />
</div>
</div>
<?php include('incl/footer.php'); ?>
</div>
</body>
</html>
I will answer your question while ignoring the security issues, mostly because I don't have much time right now.
You have one issue in your constructor, where you're assigning the contents of a $_GET['CommentID'] to one variable a the $_POST['CommentID']. This is a really bad idea, you should use either $_GET['CommentID'] or $_POST['CommentID'], using both is asking for trouble.
The reason why your comment ID isn't posting is because it's not in your HTML form. From your link, you are doing
<input type="hidden" name="id" value="<?php echo $_GET['CommentID'];?>"/>
To do what you want, it should read
<input type="hidden" name="CommentID" value="<?php echo $_POST['CommentID'];?>"/>
Change the name attribute of this input to be CommentID, read the contents of $_POST['CommentID'], and your code should work.
I have this PHP page that grabs the response's from a form (well i hope it does) and then inputs the data into a table. I then echo the response from the table on the same page. Im using ajax on the form page to send over the form values and on success of the ajax call load the data into a div. this is all done without a refresh, however no information is being sent and it just refresh's the page
my php page -
<?php
$comment_name = $_POST["name"];
$comment_body = $_POST["comment"];
$film_no = $_POST["hidden"];
echo $comment_name;
echo $comment_body;
echo $film_no;
// Connects to your Database
mysql_connect("localhost", "****", "****") or die(mysql_error());
mysql_select_db("ignitet1_CheckFilm") or die(mysql_error());
$query1 = "INSERT INTO film_comments (comments_id,film_no,name,comment)
VALUES ('','$film_no', '$comment_name','$comment_body')";
$runquery1 = mysql_query($query1)or die(mysql_error());
$getComments = mysql_query("SELECT * FROM film_comments where film_no = '$film_no'")
or die(mysql_error());
while($info = mysql_fetch_array($getComments))
{
echo "<p>";
echo $info['name'];
echo ":</p>";
echo "<p>";
echo $info['comment'];
echo "</p>";
echo "<p>Comment posted on:";
echo $info['timestamp'];
echo "</p>";
echo "</div>";
?>
my form and javascript code
<form id="ContactForm2" onsubmit="return submitForm()" >
<div> <input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="body">Comment Body</label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="submit" id="comment" class="button" value="Submit" />
<div class="form_result"> </div>
</form> </div>
<script>
function submitForm() {
$.ajax({type:'POST', url: 'comment.php', data:$('#ContactForm2').serialize(), success: function(response) {
$('#ContactForm2').find('.form_result').html(response);
}});
}
Everything i try seems to not work. help would be much appreciated! :)
Try this with your php its working fine for me.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#sub').click(function(){
$.ajax({type:'POST', url: 'comment.php', data:$('#ContactForm2').serialize(), success: function(response) {
$('#ContactForm2').find('.form_result').html(response);
}});
});
})
</script>
<body>
<div>
<form id="ContactForm2"> > <input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="comment">Comment Body</label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="button" id="sub" class="button" value="Submit" />
<div class="form_result"> </div>
</form> </div>
</body>
</html>