This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I am new to PHP and playing around with it. I have following code in my php file.
$output = "<div style='display:none'>
<div class='contact-top'></div>
<div class='contact-content'>
<h1 class='contact-title' style='text-align:center'>Write a Testimonial:</h1>
<div class='contact-loading' style='display:none'></div>
<div class='contact-message' style='display:none'></div>
<form action='#' style='display:none'>
<label for='contact-name'>*Name:</label>
<input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
<label for='contact-email'>*Email:</label>
<input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />";
if ($extra["form_subject"]) {
$output .= "
<label for='contact-subject'>Subject:</label>
<input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1003' />";
}
$output .= "
<label for='contact-message'>*Message:</label>
<textarea id='contact-message' class='contact-input' name='message' cols='40' rows='4' tabindex='1004'></textarea>
<br/>";
if ($extra["form_cc"]) {
$output .= "
<label> </label>
<input type='checkbox' id='contact-cc' name='cc' value='1' tabindex='1005' /> <span class='contact-cc'>Send me a copy</span>
<br/>";
}
$output .= "
<label> </label>
<button type='submit' class='contact-send contact-button' tabindex='1006'>Send</button>
<button type='submit' class='contact-cancel contact-button simplemodal-close' tabindex='1007'>Cancel</button>
<br/>
<input type='hidden' name='token' value='" . smcf_token($to) . "'/>
</form>
</div>
</div>";
echo $output;
When I am trying to run the code, though the model box is appearing but it also showing a php error.
below is the error message I am getting
Notice: Undefined index: form_cc in F:\wamp\www\blog\wordpress\wp-content\plugins\demo\contact.php on line 56
Any idea what is going wrong?
Because the array extra has no index called form_cc. Do a var_dump of the array extra, so you can see where your problem lies. Also use the isset() and empty() methods.
Simple, it seems that you do not have a definition of this index in the array, in this case, it seems that you are not getting the value of input
In this case simply use:
if (!empty($extra["form_cc"])) {
...
}
empty() will check existence of key/index in the array and also whether value is set or not.
The problem is that in the $extra array you haven't a position (index) called "form_cc". Thus, you should use
if (! empty($extra["form_cc"]))
{
// do stuff
}
if (isset($extra["form_cc"])) {
...
}
Will remove the warning message, this is a good habit to check isset()
Related
In the checkout page I want to add separate images instead of each radio button. I found how to do this here.
The problem here is that I cannot add a separate ID or class to the label element and it seems that is required to replace the radio buttons with an actual image. Below what I got so far. I cannot target the labels specifically as shown on the GitHub thread.
Does anyone have an idea how to add a class using PHP or JavaScript maybe? Or any other way for that matter.
Thanks in advance!
Here the HTML:
<span class="woocommerce-input-wrapper">
<input type="radio" class="input-radio " value="Banana" name="radioButtonForm" id="radioButton1">
<label for="radioButton1" class="radio ">Banana</label>
<input type="radio" class="input-radio " value="Apple" name="radioButtonForm" id="radioButton2">
<label for="radioButton2" class="radio ">Apple</label>
<input type="radio" class="input-radio " value="Pear" name="radioButtonForm" id="radioButton3">
<label for="radioButton3" class="radio ">Pear</label>
<input type="radio" class="input-radio " value="Tomato" name="radioButtonForm" id="radioButton4">
<label for="radioButton4" class="radio ">Tomato</label>
</span>
And here the CSS:
.woocommerce-input-wrapper input{
margin:0;padding:0;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
}
#radioButton1 .radio{
background-image:url(http://i.imgur.com/SJbRQF7.png);
}
#radioButton2 .radio{
background-image:url(http://i.imgur.com/lXzJ1eB.png);
}
#radioButton3 .radio{
background-image:url(http://i.imgur.com/SJbRQF7.png);
}
#radioButton4 .radio{
background-image:url(http://i.imgur.com/lXzJ1eB.png);
}
We can use preg_replace to target our labels tag and add our classes.
<?php
add_action( 'wp_loaded', function() {
function wp_custom_radio_add_class( $subject ) {
if( ! is_admin() && is_checkout() || is_page( 'checkout' ) ) {
$search = [
'/<label for="radioButton1" class="radio ">/',
'/<label for="radioButton2" class="radio ">/',
'/<label for="radioButton3" class="radio ">/',
'/<label for="radioButton4" class="radio ">/',
// ... etc.
];
$replace = [
'<label for="radioButton1" class="radio label_custom_class_1">',
'<label for="radioButton2" class="radio label_custom_class_2">',
'<label for="radioButton3" class="radio label_custom_class_3">',
'<label for="radioButton4" class="radio label_custom_class_4">',
// ... etc.
];
$subject = preg_replace( $search, $replace, $subject );
return $subject;
};
};
ob_start( 'wp_custom_radio_add_class' );
} ); ?>
If I understood your problem correctly, you don't needd to necessarily add custom ids or classes to your markup. Switching your CSS selectors to use the + -sibling selector should suffice. I made a JSFiddle to demonstrate this kind of solution:
https://jsfiddle.net/6rsf79xz/
while my first steps with wp nonce field i tried the "Basic Examples" from https://developer.wordpress.org/reference/functions/wp_nonce_field/
it says there: "simplest implementation which omits all arguments"
at the bottom of my htdocs/wp-content/plugins/abcd-plugin/abcd-plugin.php
i wrote:
function hi_in_wp_head() {
?>
<form name="f1">
<input name="i1" value="hi_in_wp_head">
<input type="submit" name="s1">
<?php wp_nonce_field('name_of_your_action', 'name_of_your_nonce_field'); ?>
</form>
<?php
if(wp_verify_nonce($_REQUEST['name_of_your_nonce_field'], 'name_of_your_action')){
// Nonce is matched and valid. do whatever you want now.
} else {
// Invalid nonce. you can throw an error here.
die("ups 19-02-28_17-09");
}
}
function hi_in_footer() {
echo '<h1>hi_in_footer</h1>';
}
complete source:
https://gist.github.com/f9f0a853f0a71c5a2055b88802a1010c
this looks like this in the web browser:
<meta name="generator" content="WordPress 5.0.3" />
<form name="f1">
<input name="i1" value="hi_in_wp_head">
<input type="submit" name="s1">
<input type="hidden" id="name_of_your_nonce_field" name="name_of_your_nonce_field" value="5a82357118" /><input type="hidden" name="_wp_http_referer" value="/wordpress/alecaddd-plugin.php" /> </form>
<br />
<b>Notice</b>: Undefined index: name_of_your_nonce_field in <b>G:\Bitnami\wordpress-5.0.3-2\apps\wordpress\htdocs\wp-content\plugins\alecaddd-plugin\alecaddd-plugin.php</b> on line <b>89</b><br />
ups 19-02-28_17-09
Undefined index: name_of_your_nonce_field during first basic example with
I do not know where the error comes from. what i could do?
As the error message states, $_REQUEST['name_of_your_nonce_field'] isn't set. You need to make sure it's set before using it:
function hi_in_wp_head() {
?>
<form name="f1">
<input name="i1" value="hi_in_wp_head">
<input type="submit" name="s1">
<?php wp_nonce_field('name_of_your_action', 'name_of_your_nonce_field'); ?>
</form>
<?php
if(isset($_REQUEST['name_of_your_nonce_field']) {
if(wp_verify_nonce($_REQUEST['name_of_your_nonce_field'], 'name_of_your_action')){
// Nonce is matched and valid. do whatever you want now.
} else {
// Invalid nonce. you can throw an error here.
die("ups 19-02-28_17-09");
}
}
}
$_REQUEST['name_of_your_nonce_field'] will be set after your form gets submitted. That's why you need the extra check.
Hi i am new to wordpress plugin development. i have issues with settings api. please help if you can.
Below is my problem.
The problem i am facing is the last record which is link it is updating in database but the title text is not updating in database.
So please help me find solution. Thank you.
function load_plugin() {
add_settings_section('plugin_main', '<h1>Ticker Settings</h1>', 'plugin_section_text', 'plugin');
add_settings_field('plugin_text_string', 'Title text', 'plugin_setting_string', 'plugin', 'plugin_main');
add_settings_field('post_title_link', 'Link', 'plugin_link_setting', 'plugin', 'plugin_main');
register_setting('plugin_options', 'plugin_options');
register_setting('post_title_link', 'post_title_link');
}
function plugin_section_text() {
echo '<p>Change your post ticker title and give link to.</p>';
}
function plugin_setting_string() {
echo "<input id='plugin_text_string' name='plugin_options' size='40'
type='text' value='" . get_option('plugin_options') . "' />";
}
function plugin_link_setting() {
echo "<input id='post_title_link' name='post_title_link' size='40'
type='text' value='" . get_option('post_title_link') . "' />";
}
add_action('admin_init', 'load_plugin');
function post_ticker_setting() {
?>
<div class="wrap">
<form action="options.php" method="post">
<?php
settings_fields('plugin_options');
settings_fields('post_title_link');
do_settings_sections('plugin');
?>
<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
</form>
</div>
<?php
}
You have to register all settings with register_setting (https://codex.wordpress.org/Function_Reference/add_settings_field). The register_setting lines you show don't correspond to your fields.
Also sections should be declared after the fields are registered (https://codex.wordpress.org/Function_Reference/register_setting)
In your code, the following lines
<?php
settings_fields('plugin_options');
settings_fields('post_title_link');
do_settings_sections('plugin');
?>
don't correspond to the declarations above:
add_settings_section('plugin_main', ...
add_settings_field('plugin_text_string', ...
add_settings_field('post_title_link', ...
Only the 'post_title_link' has the same name. Which is probably why it is the only one updating.
If I want to get $_POST['username'] I write $this->request->getPost('username');. But how I must write to get $_POST['profile']['username']?
$this->request->getPost('profile')['username'];
To be certain to avoid invalid key errors:
$profile = $this->request->getPost('profile');
$username = isset($profile['username']) ? $profile['username'] : null;
It's important to setup your form properly. You don't use [ ] around 'profile.' The php side won't know what to do with it if you post [profile][username]. It has to be profile[username]
<input type="text" name="profile[username]" value="jsmith" />
<input type="text" name="profile[password]" value="******" />
<?php
$profile = $this->request->getPost('profile');
echo $profile['username'];
?>
Output: "jsmith"
For multidimensional you would add a key of your own to base it on.
<input type="text" name="profile[first][username]" value="jsmith" />
<input type="text" name="profile[first][password]" value="******" />
<?php
$profile = $this->request->getPost('profile');
echo $profile['first']['username'];
?>
Output: "jsmith"
This is no way. Because, I use $this->request->getPost('useremail', 'email') for checking post data.
I have 2 Problem:
1- Picture Path "C:\pic\1.jpg" not added
2- Also i want make a copy of 1.jpg to mysite/img
<Form action="addgallery.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" />
<input type="submit" name="smbit" value="Save" /></Form>
<?php require_once('db.php');
if($_POST['smbit']){
$name=basename($_FILES['file_upload']['name']);
$t_name=$_FILES['file_upload']['tmp_name'];
$dir='img';
if(move_uploaded_file($t_name,$dir."/".$name))
{
if(mysql_query("insert into pics (pid,pfile) values (' ','$_FILES[pfile]')"))
echo 'File Upload Sucessfully';
}
else
{
echo 'Upload Failed!';
}
}
?>
$filep="img/".$_FILES["file_upload"]["name"];
copy($_FILES["pfile"]["tmp_name"],$filep); mysql_query("INSERT INTO
pics(pid,pfile) VALUES(' ','$filep')",$cn);