session can't be set in a class function - php

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().

Related

prevent page from refreshing after clicking a button

After a button is clicked, the page refreshes, which interferes with further functions of the page.
I tried changing the type from "submit" to "button", but then the button doesn't work at all. I also tried to return false the functions.
My simple code goes like this
<html>
<body>
<form method="Get" action="">
<input type="submit" name="buttonV" onclick="function1()"> <br>
<input type="submit" name="buttonS" onclick="funciton2()">
</form>
</body>
</html>
<?php
if ($_GET) {
if (isset($_GET['buttonV'])) {
function1();
} elseif (isset($_GET['buttonS'])) {
function2();
}
}
function function1()
{
// do stuff
}
function function2()
{
// do stuff
}
?>
What should I do in order to prevent the page to refresh?
I saw you are using php. I don't you use javascript.
You can use 'event.preventDefault()'.

Can't get values from textbox to class

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>

I'm doing something wrong but I don't know what <.<

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

how to call php function from submit button?

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;
}
?>

Any idea how to build a php string manipulation app using forms?

I want to build an string/text manipulation app that:
An app consists of a form
User inputs text there as a string using
Click a button - which usues $_POST method to manipulate a string using oop method to :
Example method assigned to a button:
public function revers_sentence() {
$this->string = strrev($this->string);
return $this;
}
Then the manipulated string is displayed in the same form.
User can do another manipulation with a converted string
How to assign method to a button to trigger the function and display results in the same form?
Can it be achived in a single php file to send and get a result?
(I want to store my classes in a seperate file)
Any help/idea Appreciated - any wise guy?
edit:
<?php
require_once('mod.php');
$string='';
if (isset($_POST['string']))
$string=$_POST['string'];
if (isset($_POST['DoStuff']))
{
//$string = reverse_1_word($string);
**$string->reverse();**<-------------------------------Fatal error:---------
}
if (isset($_POST['DoOtherStuff']))
{
$string = doOtherStuffWithThisString($string);
}
?>
Fatal error: Call to a member function odwroc() on a non-object on line 14 so how to make every new string an object ?
I would do something like that:
<?php
Class MyString{
private $string='';
function __construct(){
if (isset($_POST['string']))
$this->string=$_POST['string'];
}
function doStuffWithThisString(){
$this->string=$this->string.'!';
}
function doOtherStuffWithThisString(){
$this->string=$this->string.'!!!';
}
function getString(){
return $this->string;
}
}
$myString = new MyString();
if (isset($_POST['DoStuff']))
{
$myString->doStuffWithThisString();
}
if (isset($_POST['DoOtherStuff']))
{
$myString->doOtherStuffWithThisString();
}
?>
<form action="" method="post">
<!-- blank action attribute will post form to the current page-->
<input type="text" value="<?=$string->getString()?>" name="string" />
<!-- <?=$string->getString()?> is the same as <?php echo $string->getString(); ?> -->
<input type="submit" value="Do Stuff" name="DoStuff" />
<input type="submit" value="Do Other Stuff" name="DoOtherStuff" />
</form>
You could have a form like this :
<form action="currentFile.php" method="post">
<input type="text" value="myValue" name="text" />
<input type="submit" value="Reverse !" name="reverse" />
<input type="submit" value="Other manipulation" name="otherManip" />
</form>
When you receive the data, you can do this :
if ( isset($_POST['reverse']) ) {
// User clicked on the reverse button
} elseif ( isset($_POST['otherManip']) ) {
// User clicked on the other button
}
to achive the send and get in a single PHP file you have to set the forms action to the actual site you are on. I do this ba writing <form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post"> FORMFIELDS AND DATA </form>
To work with the returned values you just have to build an if-statement, that checks for the passed values if (isset($_POST['buttonname'])){ //DO SOMETHING}

Categories