I am trying to implement the creating topic part in forum
Why I can't use the another php file, say b.php to get data that sent from a.php?
$topic=$_POST['title'];
$detail=$_POST['content'];
$name=$_POST['username'];
Errors show message that undefined index at these 3 inputs.
Because you are calling this script without sending POST data.
Use it in following way:
$topic = empty($_POST['title']) ? null : $_POST['title'];
$detail = empty($_POST['detail']) ? null : $_POST['detail'];
$name = empty($_POST['name']) ? null : $_POST['name'];
It will avoid errors and if you just request script without POSTing, variables will contain null values
Related
I am trying to send data from AngularJS to PHP.
I am sending the data from the controller.js of AngularJS.
I am receiving the data through PHP. The data comes well for others variables.
However, I don't know which part is the problem.
My goal is to have the PHP recognize $step_number and run through the IF-statement in PHP, but even though I send wrong data, it works!
Which means,
if the $step_number in PHP is '2', then the IF-statement in PHP should not work as shown in below. But, it runs!
I thought this was the proper way to write the code as other data are being passed to PHP.
Controller.js
$http.post("../crud/projects_update.php",{
step_number : '1', // This one, I am trying to send, but I don't know whether this is not being sent or the problem is occurred in PHP.
project_id : $scope.project_data.project_id // This works in the PHP. It is well sent.
project_title : $scope.project_data.project_title
}
PHP
require_once 'connect.php';
$data = json_decode(file_get_contents("php://input"));
$step_number = $data->step_number; // This should be received from AngularJS and if the $step_number is not matched in the IF-statement, then the function in { } should not work.
if ($step_number = '1'){
$project_id = $data->project_id;
$project_title = $data->project_title;
$conn->query("UPDATE `projects` SET
`project_title` = '$project_title',WHERE `project_id` = $project_id") or die(mysqli_error());
}
Two things need to change:-
1.Change step_number : '1', to step_number : 1,
2.Change
if ($step_number = '1'){ //this is assignment not comparison
to:-
if ($step_number == 1){ // now it's comparison
My email password script is showing error:
Notice: Undefined variable: _Post in
C:\xampp\htdocs\DreamWeaver\EMPWScript.php on line 3 Fail - Please try
again!
I define the variable, what is wrong?
<?php
#session_start();
$_SESSION['EMPW'] = $_Post['Email1'];
?>
You wrote:
$_SESSION['EMPW'] = $_Post['Email1'];
but PHP is case sensitive, so try this:
$_SESSION['EMPW'] = $_POST['Email1'];
(In other words, POST has to be uppercase).
You can never be sure if it is POST or which variable type it is, so it could be better to always check and cast to the expected type.
$empw = (string) ($_POST['email] ?? '');
//here would be nice to throw some exception if email empty
$_SESSION['EMPW'] = $_POST['Email1'];
my QCubed PHP class "Project" has a property called "Finished" which is object of QDateTime and data type "datetime" in MySQL database. I need to save NULL into database when user leaves this field blank in HTML
<input type="datetime-local" name="Finished" />
but I cannot find a way for it since PHP always throws Internal Server Error 500
if ($_GET["Finished"] != "") $objProject->Finished = new QDateTime($_GET["Finished"]);
else $objProject->Finished = ???
Will be glad for any help.
This will work.
$dataobject = new TableName();
$objProject->Finished = null;
$dataobject->Save();
I'm having a problem with Flash AS3 and PHP 5.3.29
I have a var that I want to pass to PHP. This var will in certain cases be undefined or will have a value.
if(!isset($_POST['varName']) || empty($_POST['varName'])){
$name = ">0";
}
else{
$name = "=".$_POST['varName'];
}
AS3 code:
var myVarsReq:URLRequest=new URLRequest(returnQuery);
var phpMyVarsVar:URLVariables = new URLVariables();
myVarsReq.method = URLRequestMethod.POST;
phpMyVarsVar.varName = varName; //varName = undefined;
myVarsReq.data = phpMyVarsVar;
phpMyVarsVar.sendRequest = "getResults";
I use Charles to trace the outgoing data from Flash and when the varName = undefined PHP !isset() is not picking it up as not being set. If I force the varName varName = "" then the empty() function picks it up fine. Similarly, if I give the varName a value, varName = 44 it all works fine so what am I doing wrong with isset??
OK, I echo'd the $_POST var both before and after !isset. The $POST var according to Flash Charles and the echo before the !isset was undefined, not NULL "" or "undefined", however PHP is seeing it as isset and not !isset... Why PHP is doing this? I don't know but for anyone experiencing the same issue: - my work around was to force the var as "" empty rather than undefined. Its not graceful or ellegant but it works... I'd still love to know why though!
Isset only works if the variable isnt set at all, you probably send an empty or vanName=undefined from action script
maybe just do this
if($_POST['varName']=="undefined" || empty($_POST['varName'])){
$name = ">0";
}
else{
$name = "=".$_POST['varName'];
}
For those hwo are still wondering... After some investigation and assistance from another forum it appears that there is no way that you can send a var that is not set (!isset) from flash to PHP. According to PHP it will always be set one way or another.
My findings: - When myVar is defiend as a number but not given a value
sendVartoPHP.varName = myVar:Number
is seen by PHP as string(3)"NaN"
sendVartoPHP.varName = myVar:int
is seen by PHP as string(1)"0"
sendVartoPHP.varName = myVar:String
is seen by PHP as NULL
sendVartoPHP.varName = myVar
a 'none typed' var is seen by PHP as string(9)"undefined"
sendVartoPHP.varName = ""
is seen by PHP as string(0)
sendVartoPHP.varName
not even setting the var to be sent (as above) is seen by PHP as NULL
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 3 years ago.
Notice: Undefined index: subject in /var/www/mailer.php on line 12 Notice: Undefined index: message in /var/www/mailer.php on line 13 Notice: Undefined index: from in /var/www/mailer.php on line 14 Notice: Undefined index: verif_box in /var/www/mailer.php on line 15 Notice: Undefined index: tntcon in /var/www/mailer.php on line 23 no variables received, this page cannot be accessed directly
BELOW IS THE CODE
<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
// -----------------------------------------
// The Web Help .com
// -----------------------------------------
// remember to replace you#email.com with your own email address lower in this code.
// load the variables form address bar
$subject = $_POST["subject"];
$message = $_POST["message"];
$from = $_POST["from"];
$verif_box = $_POST["verif_box"];
// remove the backslashes that normally appears when entering " or '
$message = stripslashes($message);
$subject = stripslashes($subject);
$from = stripslashes($from);
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("abhijit.infogence#gmail.com", 'TheWebHelp.com Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else if(isset($message) and $message!=""){
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
} else {
echo "no variables received, this page cannot be accessed directly";
exit;
}
?>
You're trying to access parts of a variable which don't exist. You may want to check before using them, if they exist.
The error is simply because the message $_POST array does not have a key called 'message'. It probably comes from the fact that the form has not been submitted. The error is only a notice and won't stop the program from working.
You should check what $_POST contains before you address some particular fields, take a look at the isset function. Or simply turn off display_errors ;)
It seems that you call this PHP file without submitting a form via the POST method. Make sure that your mailing form has the proper method set:
<form method="POST" action="yourfile.php">
etc.
</form>
You should also sanitize the user input before calling mail() (i. e. remove newlines and tags), otherwise you are calling for trouble.
Your $_POST and $_COOKIE arrays do not contain those indexes.
Do:
print_r($_POST);
print_r($_COOKIE);
to see what is contained in those arrays
foreach(array('subject', 'message', 'from', 'verif_box') as $val)
{
if (isset($_POST[$val]))
{
$$val = trim($_POST[$val]);
continue;
}
// some sort of error checking, like telling the end user that
// not all fields were correctly given
}
Check user-submitted data
$subject = (isset($_POST["subject"]) ? $_POST["subject"] : '');
$message = (isset($_POST["message"]) ? $_POST["message"] : '');
$from = (isset($_POST["from"]) ? $_POST["from"] : '');
$verif_box = (isset($_POST["verif_box"]) ? $_POST["verif_box"] : '');
You can even make your own function to do that
function checkPost($fieldname)
{
return (isset($_POST[$fieldname]) ? $_POST[$fieldname] : '');
}
And then do
$subject = checkPost("subject");
I recommend as well that you check required POST fields
if (!isset($_POST["xxx"]) || trim($_POST["xxx"]) == '')
{
// throw exception, display error...
}
etc.
FYI, instead of using stripslashes() to avoid "magic_quotes", you can use a simple function such as this one http://snippets.dzone.com/posts/show/5256 which will do the job for all fields.
//checking if array elements are set and changing variables values if so
$subject = isset($_POST["subject"])?$_POST["subject"]:null;
$message = isset($_POST["message"])?$_POST["message"]:null;
$from = isset($_POST["from"])?$_POST["from"]:null;
$verif_box = isset($_POST["verif_box"])?$_POST["verif_box"]:null;
// check to see if verificaton code was correct and if cookie value on 'tntcon' is set
if(isset($_COOKIE['tntcon']) && md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
Changes are on lines 12-15 and in 23.