Retrieving data from textarea php - php

I cannot seem to retrieve the data input into this text area. Also I know this code may not be secure, but I am just beginning with php.
echo '<tr><td align=right>Description:</td><td><textarea name=description form=description cols=100 rows=5></textarea></td></tr>';
$descriptionToSend = $("#description").val()
DBSubmit("INSERT INTO Conference (conferenceid,description,submission_due,review_due) VALUES ('".$_POST['conferenceName']."', '" . $descriptionToSend . "','" .$_POST['submitdeadline'] . "','" .$_POST['reviewdeadline']. "')");

Are you mixing up jquery with php ?
First, the <textarea> should reside in a <form>
After the form is submitted to php, you can access the data sent with $_REQUEST['description'] so you would have
$descriptionToSend = $_REQUEST['description'];

<html>
<head>
<title>Test</title>
</head>
<body>
<?php $name = 'nick';
?>
<form action="Test.php">
Name: <input type="text" name="name" value="<?php echo $name; ?>">
About Me: <textarea name="about" rows="5" cols="10"><?php echo $comment; ?></textarea><br/>
Click Me: <input type="submit" vaule="submit">
</body>
</html>
Use $comment to set text and then just retrieve value using $_GET['about'] in next page
<html>
<body>
<?php
$about = $_GET['about'];
echo $about;
?>
</body>
</html>

Related

Comment system won't work through php

A comment posting tutorial for a guestbook type situation I watched on the internet works fine for many others but for me I keep having
" . $name. ":
" . $content . "
"); fclose ($handle); } ?>
appear above the comment box. Also it does not post anything after doing all that in php. Here is the code:
<?php
if($_POST){
$name = $_POST ['name'];
$content = $_POST ['commentcontent'];
$handle = fopen("JackPackNetwork.html","a");
fwrite ($handle, "<b>" . $name. "</b>:<br/>" . $content . "<br/>");
fclose ($handle);
}
?>
<html>
<head>
</head>
<body>
<form action="" method= "POST">
Comments: <textarea rows= "10" cols= "30" name= "commentContent"></textarea> <br/>
Name: <input type= "text" name = "name"> <br/>
<input type= "submit" value = "Post!"> <br/>
</form>
<?php include "JackPackNetwork.html"; ?>
</body>
</html>
Specifically I want to link to JackPackNetwork.html and update the comments with the ability of someone being able to put their name on it as well. Thanks for your time and consideration.
Your code should look like this:
<?php
if($_POST){
$name = $_POST ['name'];
$content = $_POST ['commentContent'];
$handle = fopen("JackPackNetwork.html","a");
fwrite ($handle, "<b>" . $name. "</b>:<br/>" . $content . "<br/>");
fclose ($handle);
}
?>
<html>
<head>
</head>
<body>
<form action="" method= "POST">
Comments: <textarea rows= "10" cols= "30" name= "commentContent"></textarea> <br/>
Name: <input type= "text" name = "name"> <br/>
<input type= "submit" value = "Post!"> <br/>
</form>
<?php require_once("JackPackNetwork.html"); ?>
</body>
</html>
MISTAKE
array index are case sensitive so $content post request fetch should be done correctly.
Suggestion
Use require_once() instead of include().

fwrite sort by date (latest high up)

I've tried to make a comment system, by using fwrite() code. It works but I want my latest comments to list on top of the list.
Here is my code which works but it doesn't work the way I want. Latest comments appears under the list :((
<?php
if ($_POST){
$name = $_POST['name'];
$content = $_POST['commentcontent'];
$date = date("Y-m-d H:i:s");
$handle = fopen("comments.html","a");
fwrite($handle,"<b>" . $name . "<b> (" .$date . ") </b>" ."</b>:</br>" . $content . "</br>");
fclose($handle);
}
?>
<html>
<body>
<form action="" method="POST">
Comments: <textarea rows="10" cols="30" name="commentcontent"/></textarea></br>
Name: <input type="text" name="name"></br>
<input type="submit" value="post!"></br>
<?php include"comments.html";?>
</form>
</body>
</html>

Populate a certificate with information from a form on another page in php

I am creating e-learning that users will take a quiz. Upon submit they will be sent to the results page where it will show them their scores. If the user passes a link will pop up to a certificate they can print out. I cannot get the certificate to populate variables(first name, last name, etc).
Here is what I have:
index.php (home page where they take the quiz and fill out a form)
<form id="form1" name="form1" method="post" action="results.php" onsubmit="return validateForm() ;">
<label>Last Name:
<input type="text" name="lname" id="lname" tabindex="1" />
</label>
<label>First Name:
<input type="text" name="fname" id="fname" tabindex="2" />
</label>
<label>Title:
<input type="text" name="title" id="title" tabindex="3" /><br/><br/>
</label>
<?php
$quiz = new Quiz( $questions );
echo $quiz->renderQuiz();
?>
<input name="Submit" type="submit" value="Submit" />
</form>
On results.php (page that shows results and link to certificate)
<?php
$ip = $_SERVER['REMOTE_ADDR']; // employee's Ip address
$time = date("d/m/y : H:i:s", time()); // current timestamp
$email_string = create_email_string( $force_email, $_POST['email_to'] );
$questions_correct = array();
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'];
// Grades the quiz
$score = (string)round( ( count($questions_correct) / count($questions)*100), 2 );
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['test_name'] = $test_name;
$variables['score'] = $score;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Results</title>
<link rel="stylesheet" type="text/css" href="./stylesheets/quiz_stylesheet.css" />
</head>
<body>
<div id="white">
<div id="header">
<img src="./images/ucdheader.jpg" width="1000" id="header" /img>
</div>
<div id="mainContent">
<h1><span>You Scored <?php echo $score ?>%</span>
<br><?php if ($score >= 80)
echo "<a href=certificate.php >Print Certificate</a>
"?></h1>
<p>Below are your results for <?php echo $test_name; ?>. In order to pass this quiz, you must score 80% or better. If you would like to try again, click the following link.</p>
<a href='<?php echo $test_page_name ?>'>Try again</a>
<p>These results were sent to the following addresses: </p>
<ul>
<?php
$all_emailed_to = explode( "; " , $email_string);
foreach ($all_emailed_to as $email) {
echo "<li>" . $email . "</li>";
}
?>
</ul>
</div>
</div>
</body>
</html>
On certificate.php
<title>Certificate</title>
</head>
<body>
<div id="white">
<div class="btn-group btn-group-lg">
<a role="button" class="btn btn-default" href="#" data- function="print">Print Certificate</a>
</div>
<div class="certificate">
<img src="images/cert.png" width="820" height="527" id="cert" />
<div class="name">
<?php $variables['fname'] . " " . $variables['lname'] . ?>
</div>
<div class="course">
<p></p>
</div>
<div class="completion_date">
<?php $variables['time']; ?>
</div>
</div>
</div>
</body>
</html>
Thank you!
You are not passing any variables to the certificate.php page. There are several options of passing data between php pages such as:
1) Sessions
2) $_POST
3) $_GET
The easiest option in your code is using the $_GET variable. Simply use this line of code in results.php:
echo "<a href=certificate.php?fname={$variables['fname']}&lname={$variables['lname']} >Print Certificate</a>"?></h1>
Than in certificate.php retrieve the data by using:
<?php echo $_GET['fname'] . " " . $_GET['lname'] . ?>
Note 1: you will need to urlendode() the variables in the link in case someone puts special chairs in their name/last name
Note 2: It's a hackable approach. An employee could print a certificate with just any name/last name by calling a prepared url.
The best approach would be to rewrite all three pages to use Sessions: http://php.net/manual/en/features.sessions.php
Either you go for Kamran's approach and build a hidden form which will send by POST the information needed in the certificate page, either you go for a database oriented model(i.e. save users and their results and then retrieve those information to generate the certificate).
The first way is pretty unsafe since you can change the values of the fields using Firebug and send dirty unexpected input if you don't test the values.
In results.php store necessary values in SESSION:
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'], time);
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['certInfo'] = $info_to_save;
Then use it in certificate.php like this:
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$lname = $_SESSION['certInfo'][0];
$fname = $_SESSION['certInfo'][1];
$title = $_SESSION['certInfo'][2];
$time = $_SESSION['certInfo'][3];

how can I transfer data between php pages?

There is a page: login.php
I try to receive username and password via form.
login.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Giris Yap</title>
</head>
<body>
<form id="form1" name="form" method="post" action="welcome.php">
<table width="275" border="1">
<tr>
<td width="103">user; name: </td>
<td width="156"><label>
<input type="text" name= "name" />
</label></td>
</tr>
<tr>
<td>password:</td>
<td><label>
<input type="password" name="textfield2" />
</label></td>
</tr>
</table>
<p><label>
<input type="submit" name="Submit" value="submit" />
</label>
<label>
<input type="reset" name="Submit2" value="reset" />
</label>
</p>
</form>
<p> </p>
<p> </p>
</body>
</html>
After that I want to lead my page into welcome.php and says welcome ...name... But it does not work. Can anyone help me.
welcome.php:
<?php
$name = $_REQUEST['name'];
echo "Welcome"$name;
?>
you can try it.
<?php
if(isset($_POST['name']))
{
$name = $_POST['name'];
echo "Welcome ". $name;
}
?>
Try
?php
if(isset($_POST['name']))
{
$name = $_POST['name'];
echo "Welcome $name";
}
else
{
echo "Name not set";
}
?>
Try this:
<?php
// let us make sure we have a post value. we will use isset to make sure it's set.
if (isset($_POST['name'])) {
// you should scrub the $name value before displaying. Never display or work with
// raw post values.
$name = $_POST['name'];
echo "Welcome ";
// what if they didn't really put in a name?
// maybe only show it if it's longer than 2?
if (strlen($str) > '2')
echo $name;
}
}
else {
// we don't have a post value? You should redirect with error message or something.
}
?>
It is all depends on the method you have specified in your form. If your form method="post" then you need retrieve like $_POST['name']
and if your form method="GET" then you need retrieve like $_GET['name']
$name = $_POST['name']; // you have used method='post'
echo "Welcome" .$name;
Also, $_REQUEST - An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
Don't forget, because $_REQUEST is a different variable than $_GET and $_POST, it is treated as such in PHP -- modifying $_GET or $_POST elements at runtime will not affect the ellements in $_REQUEST, nor vice versa.
e.g:
<?php
$_GET['foo'] = 'a';
$_POST['bar'] = 'b';
var_dump($_GET); // Element 'foo' is string(1) "a"
var_dump($_POST); // Element 'bar' is string(1) "b"
var_dump($_REQUEST); // Does not contain elements 'foo' or 'bar'
?>
Ref: http://php.net/manual/en/reserved.variables.request.php
Try like this.
Always make use of isset() construct in this context. Since we know that your <form> method has action POST, you can make use of the $_POST instead of $_REQUEST
<?php
if(isset($_POST['name']))
{
$name = $_POST['name'];
echo "Welcome $name";
}
?>
Also, you are missing the submit button code on your <form> , Do like this
<form id="form1" name="form" method="post" action="welcome.php">
<table width="275" border="1">
<tr>
<td width="103">User; Name: </td>
<td width="156"><label>
<input type="text" name= "name" />
<input type="submit" name="submit" />
</label></td>
</tr>
</form>

I can't figure how to make this php form to work

I can't figure how can I make this simple PHP form to work.
What Am I do wrong?
<?php
$first = "firstNumber";
$second = "secondNumber";
$second * $first = "calc";
$calc = "calc";
echo("" . $_GET['$calc'] . "<br />\n");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="new.php" method="get">
<input type="text" name="firstNumber" id="first">
<input type="text" name="secondNumber" id="second">
<input style="background-color: #0094ff;" type="submit" name="calc" value="שלח" id="second">
</form>
</body>
</html>
In short
I guess you want something like
<?php
$first = $_GET["firstNumber"];
$second = $_GET["secondNumber"];
$calc = $second * $first;
echo $calc . "<br />";
?>
Expanded
But in complete, because if you run this you get warnings because you didn't send the form
<?php
if(isset($_GET['calc'])) {
$first = $_GET["firstNumber"];
$second = $_GET["secondNumber"];
$calc = $second * $first;
echo $calc . "<br />";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="new.php" method="get">
<input type="text" name="firstNumber" id="first">
<input type="text" name="secondNumber" id="second">
<input style="background-color: #0094ff;" type="submit" name="calc" value="שלח" id="second">
</form>
</body>
</html>
Example
http://codepad.viper-7.com/LZ7phd
Personally, I keep the name of the input field and the id of the input field the same. This may or may not help you.
<form action="new.php" method="get">
<input type="text" name="firstNumber" id="firstNumber">
<input type="text" name="secondNumber" id="secondNumber">
<input style="background-color: #0094ff;" type="submit" name="calc" value="שלח" id="second">
</form>
$_GET['$calc'] represents nothing here.
You must use $_GET['firstNumber'] and $_GET['secondNUmber']
You're not pulling your parameters from the $_GET stream:
$first = $_GET['firstNumber'];
$second = $_GET['secondNumber'];
$calc = $first * $second;
All your fields are passed in the $_GET stream.
$_GET['calc'] is actually your submit button by the way, not your calculation variable.
You can't use the id property of input to call the values of those fields. You must use $_GET and the name of the input field. You're also assigning variables incorrectly:
<?php
$first = $_GET[firstNumber];
$second = $_GET[secondNumber];
$calc = $second * $first;
echo $calc . "<br />\n";
?>

Categories