This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I am learning PHP and I have used the post method to submit form data and I can not know why I get those two errors:
Notice: Undefined index: search_for in \xampp\htdocs\samir\indexes.php on line 5
Notice: Undefined index: replace_with in \xampp\htdocs\samir\indexes.php on line 6
if(isset($_POST['user_input']) && isset($_POST['search_for']) && isset($_POST['replace_with'])
&& !empty($_POST['user_input']) && !empty($_POST['search_for']) && !empty($_POST['replace_with']))
echo $user_input = $_POST['user_input'];
echo $search_for = $_POST['search_for'];
echo $replace_with = $_POST['replace_with'];
You forgot to add enclosing brackets for your if statement, like so:
if(isset($_POST['user_input']) && isset($_POST['search_for'])
&& isset($_POST['replace_with']) && !empty($_POST['user_input'])
&& !empty($_POST['search_for']) && !empty($_POST['replace_with'])) {
echo $user_input = $_POST['user_input'];
echo $search_for = $_POST['search_for'];
echo $replace_with = $_POST['replace_with'];
}
the above code should do what you want.
If you are using the variable before the form as been posted, e.g $var = $_POST['var'];
It will return an error.
It is best to check if the submit button has been pressed.
Example:
if(isset($_POST['submit'])){
//form has been posted
}
Then, I would make sure that all the post variables that you will be using are set, if not throw an error.
Example:
$error = false;
//['submit'] is the button used to post the form.
if(isset($_POST['submit'])){
$testVar = (isset($_POST['testVar']) && strlen($_POST['testVar']) > 0 ? $_POST : $error[] = 'Please enter something.');
if(!$error){
//Submit for
}
else{
foreach($error as $e){
echo $e;
}
}
}
When using $ _POST or $ _GET to retrieve the variables from a form, you may encounter this error:
Notice: Undefined index 'fields of the table' in 'path of php file being executes' on line 'current line'
This error appears because of your PHP error reporting settings. Usually, it appears when your variable is not properly set. There are two ways to handle this issue:
1.Check if $_POST['action'] or $GET['action'] is set before using it. For example:
if (!isset($_POST['action'])) {//your pure stuff }
if (!isset($_GET['action'])) {//your pure stuff }
2. Suppress Notice warnings
Notice warnings could be suppressed by changing the error_reporting variable in your PHP.ini. error_reporting could be set to show all errors except those for notices and coding standards warnings: error_reporting = E_ALL & ~E_NOTICE
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
but my personal suggestion is solve the warning instead of use the 2 method
updated question answer
you haven't add enclosing brackets {} so the only one line after if will consider as in if body and your 2nd and 3rd echo will be executed whether the result of your if is true or false
it should be
if(isset($_POST['user_input']) && isset($_POST['search_for'])
&& isset($_POST['replace_with']) && !empty($_POST['user_input'])
&& !empty($_POST['search_for']) && !empty($_POST['replace_with'])) {
echo $user_input = $_POST['user_input'];
echo $search_for = $_POST['search_for'];
echo $replace_with = $_POST['replace_with'];
}
Related
I have a code which calls a boolean function.
can_process = done_recently?(load)
Here is how the done_recently function looks.
def done_recently?(load)
time_window = 10000
load['terminatedAt'] && (load['terminatedAt'] > time_window.minutes.ago.utc.iso8601)
end
In my json data, inside
"load": [{ "terminatedAt": null }]
among lots of other data . This json data is converted into a hash before calling these functions.
What will the done_recently? function return(true/false)? I am new to ruby so i am getting a little confused. please help me out.
I have tried replicating in irb but got confused midway because of some errors.
When your input is { "terminatedAt": null } then
def done_recently?(load)
time_window = 10000
load['terminatedAt'] && (load['terminatedAt'] > time_window.minutes.ago.utc.iso8601)
end
will return nil. nil is returned because the first part of load['terminatedAt'] && ... evaluates to nil which is considered falsy and there for the second part after the && will not be evaluated anymore and the nil is returned immediately.
I have a function which I'm trying to test
##described_class.expects(:foo).with(
1,
2,
<any number>
)
##described_class.bar()
so here my function bar calls foo. Is there a way to set this up where :foo's third parameter can be any number?
From the question title and the code snipped I'm assuming you're using this version of mocha.
If that's the case then you can pass a block to your with and define your expectations in there, see the docs here.
object = mock()
object.expects(:expected_method).with() { |value| value % 4 == 0 }
object.expected_method(16)
# => verify succeeds
object = mock()
object.expects(:expected_method).with() { |value| value % 4 == 0 }
object.expected_method(17)
# => verify fails
The documentation doesn't have an example with more than one parameter as input, but given Ruby's nature I'd assume something like this would work
##described_class.expects(:foo).with { |first, second, third| first == 1 && second == 2 }
I'm trying to add parameter in Jenkins groovy shell script, then wonder if groovy string interpolation can be used nested way like this.
node{
def A = 'C'
def B = 'D'
def CD = 'Value what I want'
sh "echo ${${A}${B}}"
}
Then what I expected is like this;
'Value what I want'
as if I do;
sh "echo ${CD}"
But it gives some error that $ is not found among steps [...]
Is it not possible?
Like this?
import groovy.text.GStringTemplateEngine
// processes a string in "GString" format against the bindings
def postProcess(str, Map bindings) {
new GStringTemplateEngine().createTemplate(str).make(bindings).toString()
}
node{
def A = 'C'
def B = 'D'
def bindings = [
CD: 'Value what I want'
]
// so this builds the "template" echo ${CD}
def template = "echo \${${"${A}${B}"}}"
// post-process to get: echo Value what I want
def command = postProcess(template, bindings)
sh command
}
In regard to the accepted answer, if you're putting values in a map anyway then you can just interpolate your [key]:
def A = 'C'
def B = 'D'
def bindings = [ CD: 'Value what I want' ]
bindings["${A}${B}"] == 'Value what I want'
${A}${B} is not a correct groovy syntax.
The interpolation just insert the value of expression between ${}.
Even if you change to the correct syntax and create $ method, the result will not be what you want.
def nested = "${${A}+${B}}"
println nested
static $(Closure closure) { //define $ method
closure.call()
}
CD will be printed.
I have a uitextfield and i have got a search option using this uitextfield.When i enter the text it works well but when i clear the text i get "object returned empty description check" when i print it using po command in console. For checking this i have used the following code:
if(_nameTextField.text !=nil || _nameTextField.text.length !=0 || ![_nameTextField.text isEqual:#""]|| _nameTextField.text != NULL || ![_nameTextField.text isEqualToString:#""]){
}
But still it enters into the loop
use && instead of ||
if(_nameTextField.text !=nil && _nameTextField.text.length !=0 && ![_nameTextField.text isEqual:#""]&& _nameTextField.text != NULL && ![_nameTextField.text isEqualToString:#""]){
}
Please use this..it help you:
if (_nameTextField.text && _nameTextField.text.length!=0)
{
}
Use like this, it will work
if(_nameTextField.text !=nil && _nameTextField.text.length > 0 ){
//...
}
Just a simple question here
I uses the following WHERE query
def instance = ClassName.where{varone == 'A' &&
vartwo == 'B' && varthree == 'C'}.list()
And it returned me the object that i wanted --> ClassName(unsaved)
But when i tried to do the following
def instance2 = ClassName.where{varone == params.varone &&
vartwo == params.vartwo && varthree == params.varthree}.list()
It returned me the following which i couldn't do anything on it --->
grails.gorm.DetachedCriteria#somenumbershere
I don't understand what are the differences between these two. I need the 2nd query to return the same object as what the first query returned.