My intention is to retrieve data from a database and have created the following functions but receive an undefined function, grades, error message. The code is as follows:
<form action="processor.php" method="post">
<h4>Question # 1</h4>
<p>What grade are you in?</p>
<label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
<label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
<label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
<label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
<h4>Question # 2</h4>
<p>What is your current GPA?</p>
<select name="gpa">
<option value="4">3.5 or above</option>
<option value="3">3.0-3.4</option>
<option value="2">2.5-2.9</option>
<option value="1">2.0-2.4</option>
<option value="0">Lower</option>
</select>
<h4>Question # 3</h4>
<p>Where do you excel the most academically?</p>
<select name="school" multiple="multiple">
<option value ="1">Mathematics</option>
<option value ="2">Literature</option>
<option value ="3">History</option>
<option value ="4">Humanities</option>
<option value ="5">Science</option>
</select>
Then the PHP file that processes the form submission:
<?php
require_once('processor.php');
class Processor {
#code
function get_response($id) {
$dsn = "mysql:host=localhost;dbname=user";
$username = "root"; // database username
$password = "stewie12"; // database password
try {
$enter = new PDO($dsn, $username, $password);
$sql = "SELECT response FROM recommendations WHERE id = ? ";
$new_item = $enter->prepare($sql);
$new_item->setFetchmode(PDO::FETCH_ASSOC);
$new_item->execute(array($id));
foreach($new_item as $nw) {
return $nw['response'];
}
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
return "";
}
function GetWeird () {
$santiago="1992";
if ($santiago="1992") {
$id = 1;
} else {
$id = 2;
}
echo $this->get_response($id);
}
function Grades () {
$grade = $_POST['grade'];
if ($grade =="1") {
echo "You're a freshmen";
} elseif ($grade == "2") {
echo "You're a sophomore";
} elseif ($grade == "3") {
echo "You're a junior.";
} elseif ($grade == "4") {
echo "You're a senior.";
} else {
echo "Something is wrong.";
}
}
function Gpa () {
$gpa = $_POST['gpa'];
if ($gpa =="1") {
echo "You strongly need to up your GPA.";
} elseif ($gpa == "2") {
echo "You're an average student.";
} elseif ($gpa == "3") {
echo "You're an above average student.";
} elseif ($gpa == "4") {
echo "You're an excellent sudent.";
} else {
echo "Something is wrong.";
}
}
function School () {
$school = $_POST['school'];
if ($school =="1") {
echo "You're into Math";
} elseif ($school == "2") {
echo "You're into Lit";
} elseif ($school == "3") {
echo "You're into history.";
} elseif ($school == "4") {
echo "You're into humanities.";
} elseif ($school == "5") {
echo "You're into science.";
} else {
echo "Something is wrong.";
}
}
And the view page:
<?php $processor = new Processor(); ?>
<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php School($school); ?></p>
To access the class function use below code,
You have to use it like this $class_variable->function_name()
<?php $processor = new Processor(); ?>
<h4>Question # 1</h4>
<p><?php $processor->Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php $processor->Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php $processor->School($school); ?></p>
Related
This is my php form i want to submit from on selected value on page load how it's possible. on page load i want to select Architect as a selected value.
<select name="with" <?php if(isset($_REQUEST['market']) && !empty($_REQUEST['market'])) { ?> onChange="if(this.value=='1'){window.location='?market=<?php echo $_REQUEST['market']; ?>&with=1'} else if(this.value=='2') {window.location='?market=<?php echo $_REQUEST['market']; ?>&with=2'} else {window.location='<?php echo $baseUrl."viewMeetings.php?market=".$_REQUEST['market']; ?>'}; <?php } else { ?> onChange=" if(this.value=='1'){window.location='?with=1'} else
if(this.value=='2') {window.location='?with=2'} else {window.location='<?php echo $baseUrl."viewMeetings.php"; ?>'}; <?php } ?>">
<option value="" <?php if(!isset($_REQUEST['with'])) { echo "selected=selected"; } ?>>ALL </option>
<option value="1" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==1) { echo "selected=selected"; } } ?>>Customer</option>
<option value="2" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==2) { echo "selected=selected"; } } ?> >Architect</option>
</select>
Try not using inline javascript. I am not the best at Javascripting but see if this works. Your description is not very clear so hopefully this is what you are looking for! The loading and so forth technically works, but whether it works like you are expecting is another story:
jQuery Libraries
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
Select Button (Add id)
<select name="with" id="with-it">
<option value="" <?php if(!isset($_REQUEST['with'])) { echo "selected=selected"; } ?>>ALL </option>
<option value="1" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==1) { echo "selected=selected"; } } ?>>Customer</option>
<option value="2" <?php if(isset($_REQUEST['with'])) { if($_REQUEST['with']==2) { echo "selected=selected"; } } ?> >Architect</option>
</select>
Script for onChange
<script>
$("#with-it").change(function() {
var ValueSet = $(this).val();
<?php if(isset($_REQUEST['market'])) { ?>
if(ValueSet == '1') {
window.location='?market=<?php echo $_REQUEST['market']; ?>&with='+ValueSet
}
else if(ValueSet == '2') {
window.location='?market=<?php echo $_REQUEST['market']; ?>&with='+ValueSet
}
else {
window.location='<?php echo $baseUrl."viewMeetings.php?market=".$_REQUEST['market']; ?>'}
<?php }
else { ?>
if(ValueSet == '1'){
window.location='?with=1';
}
else if(ValueSet =='2') {
window.location = '?with=2';
}
else {
window.location = '<?php echo $baseUrl."viewMeetings.php"; ?>';
}
<?php } ?>
});
</script>
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Below is the code for a simple form with input/output. For some reason, only question one seems to be working and questions two and three simply echo back "Something is wrong." I coded all three questions the exact same way throughout so I'm not really sure why question two and three aren't processing correctly. Any advice would be greatly appreciated.
Here is the HTML form:
<form action="processor.php" method="post">
<h4>Question # 1</h4>
<p>What grade are you in?</p>
<label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
<label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
<label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
<label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
<h4>Question # 2</h4>
<p>What is your current GPA?</p>
<select>
<option name="gpa" value="4">3.5 or above</option>
<option name="gpa" value="3">3.0-3.4</option>
<option name="gpa" value="2">2.5-2.9</option>
<option name="gpa" value="1">2.0-2.4</option>
<option>Lower</option>
</select>
<h4>Question # 3</h4>
<p>Where do you excel the most academically?</p>
<select multiple="multiple">
<option name="school" value ="1">Mathematics</option>
<option name="school" value ="2">Literature</option>
<option name="school" value ="3">History</option>
<option name="school" value ="4">Humanities</option>
<option name="school" value ="5">Science</option>
</select>
<div class="button">
<button class="btn btn-primary" input type="submit" name="submit" href="processor.php">Submit</button>
</div>
</form>
Here is the processor:
<?php
function Grades () {
$grade = $_POST['grade'];
if ($grade =="1") {
echo "You're a freshmen";
} elseif ($grade == "2") {
echo "You're a sophomore";
} elseif ($grade == "3") {
echo "You're a junior.";
} elseif ($grade == "4") {
echo "You're a senior.";
} else {
echo "Something is wrong.";
}
}
function Gpa () {
$gpa = $_POST['gpa'];
if ($gpa =="1") {
echo "You strongly need to up your GPA.";
} elseif ($gpa == "2") {
echo "You're an average student.";
} elseif ($gpa == "3") {
echo "You're an above average student.";
} elseif ($gpa == "4") {
echo "You're an excellent sudent.";
} else {
echo "Something is wrong.";
}
}
function School () {
$school = $_POST['school'];
if ($school =="1") {
echo "You're into Math";
} elseif ($school == "2") {
echo "You're into Lit";
} elseif ($school == "3") {
echo "You're into history.";
} elseif ($school == "4") {
echo "You're into humanities.";
} elseif ($school == "5") {
echo "You're into science.";
} else {
echo "Something is wrong.";
}
}
include('viewpage.php');
?>
And the view page:
<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php School($school); ?></p>
The problem is here
<select name="gpa">
<option value="4">3.5 or above</option>
To access $_POST['gpa'] <select> tag should have name property as gpa not <option> tag
For $_POST['School']
<select name="school" multiple="multiple">
<option value ="1">Mathematics</option>
also i think some think wrong in view page code. Grades,Gpa,School function are not have variable. but you call these function like this,
<?php Grades($grade); ?>
<?php Gpa($gpa); ?>
<?php Gpa($School); ?>
replace these code like below
<?php Grades(); ?>
<?php Gpa(); ?>
<?php Gpa(); ?>
and also you will get error if user not select options in question1 n question 2. you must add code to check that..
Here is what I have so far:
Age: <input type="text" name="age">
Canoe<input type="radio" name="ck">
Kayak<input type="radio" name="ck">
<input type="submit" style=" margin-bottom: 30px;">
</form>
You are <?php $_POST["age"];
if ($_POST["age"]<"11")
echo "an Atom.";
elseif ($_POST["age"]<"13")
echo "a Peewee.";
elseif ($_POST["age"]<"15")
echo "a Bantam.";
elseif ($_POST["age"]<"17")
echo "a Midget.";
elseif ($_POST["age"]<"19")
echo "a Juvenile.";
elseif ($_POST["age"]<"21")
echo "a Junior.";
elseif ($_POST["age"]<"120")
echo "a Senior.";
elseif ($_POST["age"]>"120")
echo "too old to race";
?>
I want to be able to include the canoe and kayak radio buttons in this to make the output be like: You are a Peewee canoeist and your opponents this year are: | or, you are a midget kayak er. You're opponents this year are:
Are you trying to echo the CK value into your output? Does this help?
Age: <input type="text" name="age">
Canoe<input type="radio" name="ck" value="canoeist">
Kayak<input type="radio" name="ck" value="kayakist">
<input type="submit" style=" margin-bottom: 30px;">
</form>
<?php
$ck = $_POST["ck"];
?>
You are <?php $_POST["age"];
if ($_POST["age"]<"11")
echo "a $ck Atom.";
elseif ($_POST["age"]<"13")
echo "a $ck Peewee.";
elseif ($_POST["age"]<"15")
echo "a $ck Bantam.";
elseif ($_POST["age"]<"17")
echo "a $ck Midget.";
elseif ($_POST["age"]<"19")
echo "a $ck Juvenile.";
elseif ($_POST["age"]<"21")
echo "a $ck Junior.";
elseif ($_POST["age"]<"120")
echo "a $ck Senior.";
elseif ($_POST["age"]>"120")
echo "too old to race";
?>
The values of the form radio must be canoeist and "kayakist". Just add the $ck = $_POST['ck']; and your sentence is like echo "a $ck Atom.";
You should use some variables here.
(I'm not that good at php, but I'll give it a try)
Age: <input type="text" name="age">
Canoe<input type="radio" name="ck" value="canoe"/>
Kayak<input type="radio" name="ck" value="kayak"/>
<input type="submit" style=" margin-bottom: 30px;">
</form>
$boat = "";
if ($_POST["ck"] == "canoe")
{
$boat = "canoeist";
} else if ($_POST["ck"] == "kayak")
{
$boat = "kayak er";
}
$rank = "";
You are <?php $_POST["age"];
if ($_POST["age"]<"11")
$rank = "an Atom.";
elseif ($_POST["age"]<"13")
$rank = "a Peewee.";
elseif ($_POST["age"]<"15")
$rank = "a Bantam.";
elseif ($_POST["age"]<"17")
$rank = "a Midget.";
elseif ($_POST["age"]<"19")
$rank = "a Juvenile.";
elseif ($_POST["age"]<"21")
$rank = "a Junior.";
elseif ($_POST["age"]<"120")
$rank = "a Senior.";
elseif ($_POST["age"]>"120")
$rank = "too old to race";
echo "You are " . $rank . " " . $boat;
?>
I have a problem with php & mysql, insert to database using utf-8.
first file:
addsite:
<?php
include 'header.php';
if(isset($data)) {
foreach($_POST as $key => $value) {
$posts[$key] = filter($value);
}
if(isset($posts['type'])){
if($posts['url'] == "http://" || $posts['url'] == ""){
$error = "Add your page link!";
}else if($posts['title'] == ""){
$error = "Add your page title!";
}else if(!preg_match("/\bhttp\b/i", $posts['url'])){
$error = "URL must contain http://";
}else if(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $posts['url'])){
$error = "Please do not use special characters in the url.<";
}else{
include "plugins/" . $posts['type'] . "/addsite.php";
}
}
?>
<div class="contentbox">
<font size="2">
<li>Pick the type of exchange you are promoting from the dropdown menu.</li>
<li>Set the amount of coins you wish to give per user complete(CPC).</li>
<li>The higher the amount of coins the higher the Links position.</li>
</div>
<div class="contentbox">
<div class="head">Add Site</div>
<div class="contentinside">
<?php if(isset($error)) { ?>
<div class="error">ERROR: <?php echo $error; ?></div>
<?php }
if(isset($success)) { ?>
<div class="success">SUCCESS: <?php echo $success; ?></div>
<?php }
if(isset($warning)) { ?>
<div class="warning">WARNING: <?php echo $warning; ?></div>
<?php } ?>
<form class="contentform" method="post">
Type<br/>
<select name="type"><?php $select = hook_filter('add_site_select', ""); echo $select; ?></select><br/><br/>
Link<br/>
<input name="url" type="text" value="<?php if(isset($posts["url"])) { echo $posts["url"]; } ?>"/><br/><br/>
Title<br/>
<input name="title" type="text" value="<?php if(isset($posts["title"])) { echo $posts["title"]; } ?>"/><br/><br/>
Cost Per Click<br/>
<?php if($data->premium > 0) { ?>
<select name="cpc"><?php for($x = 2; $x <= $site->premcpc; $x++) { if(isset($posts["cpc"]) && $posts["cpc"] == $x) { echo "<option selected>$x</option>"; } else { echo "<option>$x</option>"; } } ?></select><br/><br/>
<?php }else{ ?>
<select name="cpc"><?php for($x = 2; $x <= $site->cpc; $x++) { if(isset($posts["cpc"]) && $posts["cpc"] == $x) { echo "<option selected>$x</option>"; } else { echo "<option>$x</option>"; } } ?></select><br/><br/>
<?php } ?>
<input style="width:40%;" type="Submit"/>
</form>
</div>
</div>
<?php
}
else
{
echo "Please login to view this page!";
}
include 'footer.php';
?>
second file , plugin addsite.php :
<?php
header('Content-Type: text/html; charset=utf-8');
mysql_query("SET NAMES utf8");
$num1 = mysql_query("SELECT * FROM `digg` WHERE `url`='{$posts['url']}'");
mysql_query("SET NAMES utf8");
$num = mysql_num_rows($num1);
mysql_query("SET NAMES utf8");
if($num > 0){
$error = "Page already added!";
}else if(!strstr($posts['url'], 'digg.com')) {
$error = "Incorrect URL! You must include 'digg.com'";
}else{
mysql_query("SET NAMES utf8");
mysql_query("INSERT INTO `digg` (user, url, title, cpc) VALUES('{$data->id}', '{$posts['url']}', '{$posts['title']}', '{$posts['cpc']}') ");
$success = "Page added successfully!";
}
?>
when i write arabic language in the form and submit ,
it went to database with unkown language like :
Oslash;£Ø³Ù
3 Solutions in your case...
First, you can create your database in the UTF-8 charset, conversions is not needed.
Second, you can test this code...
htmlentities(urldecode( $my_var_to_utf8 ), ENT_QUOTES, 'UTF-8');
If it don't works, try this function...
function to_utf8( $string ) {
// From http://w3.org/International/questions/qa-forms-utf-8.html
if ( preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string) ) {
return $string;
} else {
return iconv('CP1252', 'UTF-8', $string);
}
}
To use...
echo(to_utf8($my_var_to_utf8));
I do not know what im doing wrong. I have been looking through other forums that say my problem might be related to not having closed curly braces closed or a short php tag <? .... I have none of these as far as I can tell. This is a form that lets you know if any fields are left blank.
<?php
if (count($_POST) > 0)
{
function check_if_field_submitted($field_to_check)
{
if (isset($_POST[$field_to_check]) && $_POST[$field_to_check] != '')
{
return TRUE;
}
else
{
return "YOU MUST FILL IN THE $field_to_check FIELD!";
}
}
//--------------------------------------------------------
$error_messages = array();
//Validate the input
//Trim the fields
$_POST['first_name'] = trim($_POST['first_name']);
$_POST['last_name'] = trim($_POST['last_name']);
$_POST['comments'] = trim($_POST['comments']);
$_POST['first_name'] = strip_tags($_POST['first_name']);
$_POST['last_name'] = strip_tags($_POST['last_name']);
$_POST['comments'] = strip_tags($_POST['comments']);
//Required fields:
if (check_if_field_submitted('first_name') !== TRUE)
{
$error_messages[] = check_if_field_submitted('first_name');
}
if (check_if_field_submitted('last_name') !== TRUE)
{
$error_messages[] = check_if_field_submitted('last_name');
}
if (check_if_field_submitted('hobbies') !== TRUE)
{
$error_messages[] = check_if_field_submitted('hobbies');
}
if (check_if_field_submitted('university') !== TRUE)
{
$error_messages[] = check_if_field_submitted('university');
}
if (check_if_field_submitted('year') !== TRUE)
{
$error_messages[] = check_if_field_submitted('year');
}
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
?>
<DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($error_messages) && count($error_messages) > 0)
{
echo "<ul>";
foreach($error_messages as $message)
{
echo "<li>$message</li>";
}
echo "</ul>";
}
?>
<h1>Register or Fail</h1>
<form method="post" action="index.php">
<fieldset>
<label>First Name</label>
<input type='text' name='first_name' value="<?php if(isset($_POST['first_name'])) { echo $_POST['first_name']; } ;?>" />
<label>Last Name</label>
<input type='text' name='last_name' value='<?php if(isset($_POST['last_name'])) { echo $_POST['last_name']; } ;?>'/>
</fieldset>
<fieldset>
<label>What are your hobbies?</label>
<input type="checkbox" name="hobbies" value="movies" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'movies') { echo "checked='checked'"; } ?> /> Movies
<input type="checkbox" name="hobbies" value="sports" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'sports') { echo "checked='checked'"; } ?> /> Sports
<input type="checkbox" name="hobbies" value="books" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'books') { echo "checked='checked'"; } ?> /> Books
<input type="checkbox" name="hobbies" value="vgames" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'vgames') { echo "checked='checked'"; } ?> /> Video Games
<input type="checkbox" name="hobbies" value="science" <?php if(isset($_POST['hobbies']) && $_POST['hobbies'] == 'science') { echo "checked='checked'"; } ?> /> FOR SCIENCE!
</fieldset>
<fieldset>
<label>What year are you?</label>
<input type="radio" name="year" value="1" <?php if(isset($_POST['year']) && $_POST['year'] == '1') { echo "checked='checked'"; } ?> /> Freshman
<input type="radio" name="year" value="2" <?php if(isset($_POST['year']) && $_POST['year'] == '2') { echo "checked='checked'"; } ?> /> Sophomore
<input type="radio" name="year" value="3" <?php if(isset($_POST['year']) && $_POST['year'] == '3') { echo "checked='checked'"; } ?> /> Junior
<input type="radio" name="year" value="4" <?php if(isset($_POST['year']) && $_POST['year'] == '4') { echo "checked='checked'"; } ?> /> Senior
</fieldset>
<fieldset>
<label>What university are you attending?</label>
<select name="university">
<option value="" <?php if(isset($_POST['university']) && $_POST['university'] == '') { echo "selected='selected'"; } ?> >Please Select an Option</option>
<option value="1" <?php if(isset($_POST['university']) && $_POST['university'] == '1') { echo "selected='selected'"; } ?> >Florida State University</option>
<option value="2" <?php if(isset($_POST['university']) && $_POST['university'] == '2') { echo "selected='selected'"; } ?> >University of Florida</option>
<option value="3" <?php if(isset($_POST['university']) && $_POST['university'] == '3') { echo "selected='selected'"; } ?> >University of Central Florida</option>
<option value="4" <?php if(isset($_POST['university']) && $_POST['university'] == '4') { echo "selected='selected'"; } ?> >University of Miami</option>
</select>
</fieldset>
<fieldset>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>
Problem:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
Answer:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
}
if (count($error_messages) < 1)
{
header("Location: success.php");
}
}
If I were you I would find an IDE with bracket matching/highlighting.
It looks like you're missing a closing brace on one of your if statements.
I think you want to change these lines:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
to this:
if (check_if_field_submitted('comments') !== TRUE)
{
$error_messages[] = check_if_field_submitted('comments');
}