My function is
function getReg($id) {
return file_get_contents('http://arcbots.com/userinfo.php?id='.$id);
}
My php for the echo is
<?php
function display()
{
echo getReg('$_POST["search"]');
}
if(isset($_POST["search"]))
{
display();
}
?>
and my form code is
<center><form method="post" />
<input type="text" name="search">
<input type="submit" value="search" name="submit">
</form></center>
I know I'm doing something very wrong. If the code was to work when I search "1111211137" it should return "LeJordannn" Its returning "Danjr4149000099" meaning I'm doing something wrong.
Single quotes in PHP tell PHP not to parse for those variables, so it assumes everything passed to getReg is a literal. Redo your function like this:
<?php
function display()
{
echo getReg($_POST['search']);
}
if(isset($_POST["search"]))
{
display();
}
?>
try
<?php
function display($term)
{
echo getReg($term);
}
if(isset($_POST["search"]))
{
display($_POST["search"]);
}
?>
do it this way and your function can be reused with different values
Related
I try to create a function to filter the input field after submit
database connection
include("includes/connect.php");
this function is like that
<?php
function filter($x){
global $conn;
$y=strip_tags(htmlspecialchars(htmlentities(mysqli_real_escape_string($conn,$x))));
}
?>
Form
<form method="post">
<input type="text" name="txt_name">
<input type="submit" name="add" value="Add">
</form>
Code
<?php
if(isset($_POST['add'])){
$name= filtration($_POST['txt_name']);
echo $name;
}
?>
When i try to print the $name after filtration i don't have any result
i try to do that
strip_tags(htmlspecialchars(htmlentities($x)))
and i have an output
but for me i want to use mysqli_real_escape_string
how can i solve this problem??!
You'll need to return the value from your function. :)
function filter($x) {
// do the cleaning of your string
return $y;
}
Replace
$name= filtration($_POST['txt_name']);
with
$name= htmlspecialchars(filter_var(trim($_POST['txt_name']), FILTER_SANITIZE_STRING), ENT_QUOTES );
I want to make a basic calculator. Every thing is set, the textboxes and buttons. It needs to be OOP, but that gives me problems. It worked before until I tried to do it in OOP.
I don't get errors but when ever I try to let the code calculate a sum, it results the answer as 0. Its probably because the class doesn't get the values from the textboxes, but I don't know how to fix it.
Code of the class where the calculation needs to be:
class CountUp
{
public static $_sum;
public static $number1;
public static $number2;
public function __construct()
{
self::$_sum;
self::$number1;
self::$number2;
}
public function getnumber1()
{
self::$number1 = ($_POST['number1']);
return self::$number1;
}
public function getnumber2()
{
self::$number2 = ($_POST['number2']);
return self::$number2;
}
public static function getsum()
{
$_sum = self::$number1 + self::$number2;
return $_sum;
}
}
Sorry if this is a stupid question, I'm bad at php.
EDIT: This is where the values are supossed the come from:
<html>
<head>
</head>
<body>
<form name ="btw calculate" method="post" action="test2.php"><br/>
enter a number <br/>
<input type="tekst" name="number1" value=""><br/>
<input type="submit" name="plus" value="+">
<input type="submit" name="retract" value="- "><br/>
<input type="submit" name="divide" value="/ ">
<input type="submit" name="multiply" value="* "><br/>
enter a second number <br/>
<input type="tekst" name="number2" value=""><br/>
</form>
</body>
</html>
EDIT2: I'm an idiot, forgot to add this part to this question:
<?php
include("plus.class.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (isset($_POST['plus']))
{
echo CountUp::getSum();
}
else
{
//still empty for now
}
}
?>
You are mixing up static functions which are related to the class definition.
And object instance functions, which are related to each 'new' 'instance' of a class you create.
Here in an input form and the class adding the $_POST array values together.
<?php if (!empty($_POST['number1'])) {
// object instance version
class CountUp
{
private $number1 = 0;
private $number2 = 0;
public function setNumber1($number)
{
$this->number1 = $number;
}
public function setNumber2($number)
{
$this->number2 = ($number)
public function getSum()
{
$_sum = $this->number1 + $this->number2;
return $_sum;
}
}
$addTwoNumbers1 = new CountUp();
$addTwoNumbers1->setNumber1($_POST['number1']);
$addTwoNumbers1->setNumber2($_POST['number2']);
?>
<p> The answer of: <?= $_POST['number1']?> + <?= $_POST['number2'] ?> = <?= $addTwoNumbers1->getSum(); ?>
<?php
}
?>
<html>
<head>
</head>
<body>
<form name ="btw calculate" method="post" action=""><br/>
enter a number <br/>
<input type="text" name="number1" value=""><br/>
<p>will be added to...</p>
enter a second number <br/>
<input type="text" name="number2" value=""><br/>
<input type="submit" name="plus" value="add the two numbers together...">
</form>
</body>
</html>
my filename is contacts.php that have two submit buttons;i want that if insert button is pressed insert function is called and if select is pressed select is called.i have written following code:
//contacts.php
<?php
if(isset($_REQUEST['select']))
{
select();
}
else
{
insert();
}
?>
<html>
<body>
<form action="contacts.php">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select()
{
//do something
}
function insert()
{
//do something
}
?>
but it is not working .please help
<?php
if (isset($_REQUEST['insert'])) {
insert();
} elseif (isset($_REQUEST['select'])) {
select();
}
Your code is calling insert() even if no button is clicked, which will happen when the page is first displayed.
use post method because it is secure
//contacts.php
<?php
if(isset($_POST['select']))
{
select();
}
else
{
insert();
}
?>
<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select()
{
//do something
}
function insert()
{
//do something
}
?>
If you are using return inside function to return the result , you have to use echo to print the result while calling function.
if(isset($_REQUEST['select']))
{
echo select();
}
elseif(isset($_REQUEST['insert']))
{
echo insert();
}
As has been described by several people (summarizing the previous comments), you have two options.
The first is to send the data via POST or GET to the server directly and reserve (refresh) the page based on whatever you do inside select() and insert().
While this is not the right place for a POST v GET discussion, convention is to use POST when sending data to the server. POST is slightly more secure because the information is not stored in the browser. Read more about the two here: http://www.w3schools.com/tags/ref_httpmethods.asp
The second option is to use AJAX to accomplish your task without refreshing the web page. In short, AJAX uses Javascript methods that you place on your page to communicate with your server, thus avoiding the need for the PHP on the server to actually change anything on the page (which would require a refresh). A code example of AJAX can be found here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first
<?php
$insert = $_POST['insert'];
$select = $_POST['select'];
if ($insert) {
insert();
}
if ($select) {
select();
}
else {
echo 'press any button...';
}
?>
<html>
<body>
<form action="contacts.php" method="post">
<input type="text" name="txt"/>
<input type="submit" name="insert" value="insert" />
<input type="submit" name="select" value="select"/>
</form>
<?php
function select() {
echo 'you pressed the [select] button';
exit;
}
function insert() {
echo 'you pressed the [insert] button';
exit;
}
?>
This is a little part of a college website project I have and I've ran into this issue. I hope you can help me with it.
I've made a small representation of this issue so it's easier to read.
What I'm trying to do here is:
doubt1.php = shows a form.
doubt2.php = shows form with the values from doubt1.php for confirmation.
doubt3.php = saves values to database.
class.php = library of clases(only name).
The problem is that it saves empty values at doubt3.php.
If I skip doubt2.php and redirect the form from doubt1.php to doubt3.php I have no problem at all, it saves successfully.
These are the codes:
doubt1.php
<html>
<body>
<form name=f action=doubt2.php method=post>
<input name=name value="Hello";>
<input type=submit>
</form>
</body>
</html>
doubt2.php
<html>
<head>
<?php
$y=$_REQUEST['name'];
?>
</head>
<body>
<form name=f action=doubt3.php method=post>
<input name=name value="<?php echo $y; ?>" disabled>
<input type=submit>
</form>
</body>
</html>
doubt3.php
<?php
$c=mysql_connect("localhost","root","root");
mysql_select_db("doubtdb");
if(!mysql_select_db("doubtdb")){
$q1="create database doubtdb";
$q2="use doubtdb";
$q3="create table data(name varchar(10))";
mysql_query($q1,$c);
mysql_query($q2,$c);
mysql_query($q3,$c);
mysql_select_db('doubtdb');
}
include "class.php";
$obj=new data($_REQUEST['name']);
$obj->save($c);
echo "Saved";
?>
class.php
<?php
class data{
private $name;
function __construct($name){
$this->name=$name;
}
function set_name($name){
$this->name=$name;
}
function get_name(){
return $this->name;
}
function save($c){
$q="insert into data values('$this->name')";
mysql_query($q,$c);
mysql_close($c);
}
}
?>
disabled input is not submited with the form.
try "readonly"
http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1
I tried to build a multi-language site, but the problem the variable can't be set after clicking the submit button for choosing language:
<form action="<?php $aradown->make_lang(); ?>" method="post">
<input type="submit" name="en" value="english" >
<input type="submit" name="ar" value="arabic" >
</form>
Class function code:
public function make_lang(){
if($_POST['en']){
$_SESSION['lang_en'];
}
if($_POST['ar']){
$_SESSION['lang_ar'];
}
}
public function check_lang(){
if(isset($_SESSION['lang_en'])){
$lang="english";
}
if(isset($_SESSION['lang_ar'])){
$lang="arabic";
}
$path=dirname(__FILE__)."/languages/".$lang.".php";
return $path;
}
And this is the code to use:
include('includes/core.class.php');
$aradown= new aradown;
$lang_file=$aradown->check_lang();
include($lang_file);
I tried to print the result of $lang_file, but the $lang var is empty.
C:\AppServ\www\aradown-new\includes/languages/.php
Any thing missing?
You need to actually set the variables to something.
if($_POST['en']){
$_SESSION['lang_en'] = true;
}
if($_POST['ar']){
$_SESSION['lang_ar'] = true;
}
As well as start the session using session_start().