I want to get array value from get_option following this answer to add more but its not working for me. I can't find where I have gone wrong. It works when I am not using array.
Working code (without array):
add_action('admin_init', function() {
register_setting('my-test', 'option1');
register_setting('my-test', 'option2');
register_setting('my-test', 'option3');
});
function my_function() {
?>
<div class="wrap">
<h1>Dashboard</h1>
<form action="options.php" method="post">
<?php
settings_fields('my-test');
do_settings_sections('my-test');
?>
<input type="text" name="option1" value="<?php echo esc_attr( get_option('option1') ); ?>"" />
<?php submit_button(); ?>
</form>
</div>
<?php
}
Then I want to use array to get_option. I followed the answer I mention above but it gives me error. Please see this code below.
Not working code (with array):
add_action('admin_init', function() {
register_setting('my-test', 'option1');
register_setting('my-test', 'option2');
register_setting('my-test', 'option3');
});
function my_function() {
$new_option = esc_attr(get_option('option2'));
?>
<div class="wrap">
<h1>Dashboard</h1>
<form action="options.php" method="post">
<?php
settings_fields('my-test');
do_settings_sections('my-test');
?>
<input type="text" name="option1" value="<?php echo esc_attr( get_option('option1') ); ?>"" />
<input type="text" name="option2[first]" value="<?php echo $new_option['first']; ?>" />
<input type="text" name="option2[second]" value="<?php echo $new_option['second']; ?>" />
<?php submit_button(); ?>
</form>
</div>
<?php
}
Am I missing something? Any help is much appreciated. Thanks in advance.
Remove esc_attr function in the below line. This function takes string as input and returns string. Since you are dealing with array this will cause error.
$new_option = get_option('option2');
You should use the function where you are actually outputting the value.
<input type="text" name="option2[first]" value="<?php echo esc_attr( $new_option['first'] ); ?>" />
Hope it helps. :)
Related
Hello my English is not so good.
I need to load the data from place HTML file 1 to $logos, but that doesn't work.
I don't know how to do it.
I believe the problem is here: esc_attr($feed->readmore_logon);.
File 1 :
<div class="field rssap-field-container1">
<p><label for="rssap-display-readmore1">
<input type="checkbox" <?php if ($feed->logo_readmore) : ?>checked="checked"<?php endif; ?> value="1" name="logo_readmore" id="rssap-display-readmore1" />
<?php esc_html_e('Display link to source', 'rss-autopilot'); ?>
</label></p>
<input type="text" size="50" name="readmore_logon" value="<?php echo esc_attr($feed->readmore_logon); ?>" />
<span title="<?php esc_html_e('HTML allowed. Use %LINK% placeholder to add URL', 'rss-autopilot'); ?>" class="rss-tooltip">?</span>
</div>
File 2, the esc_attr($feed->readmore_logon); doesn't work.
Can somebody help?
$logos = esc_attr($feed->readmore_logon);
....
update_post_meta($postId, 'haus', $logos);
thx an everyon ho give a ansser !! i get it work thets is the code
$logos = $this->readmore_logon;
Hi I'm pretty new to PHP I need help with this small form I've made on my page.
I have a single checkbox which is in a loop. I seem to get only a single checkbox value passed. Here's my form and php code:
Form:
<?php if($show_captions) {
$pic_path=$album."/".$caption;
?>
<div class="caption" align="center">
<form name="store" id="str" action="store.php" method="get">
<input type="checkbox" name="pic[]" value="<?php echo $pic_path ?>"/>
</form>
</div>
<?php } ?>
store.php:
<?php
$path=$_GET['pic'];
if(isset($_GET['pic']))
{
foreach($path as $pic){
echo $pic;
}
}
?>
I'm a complete beginner with web development and I'm facing a problem. I have a navigation bar, with a search form that 'gets' to search.php
Sample URL : http:/....search.php?s=asdas
I would want to get the value of $_GET['s'] and place it in a textbox with the id = 'filter-searchbox'
My code so far goes like this :
<?php
if(isset($_GET['s'])){
$searchWord = $_GET['s'];
?>
<script>
document.getElementById('filter-searchbar').value =<?php $searchWord ?> ;
</script>
<?php }
?>
<h3> <div class="label label-default"> Search for Events </div></h3>
<BR>
<div id="filter" class="col-md-3">
<input id='filter-searchbar' name='searchWord' type="text" class="form-control" placeholder="Search Events..." value= "" ><br>
Thank you very much!
Rewritten the code, Try this,
<?php
$searchWord="";
if(isset($_GET['s'])){
$searchWord = $_GET['s'];
}
?>
<input id='filter-searchbar' name='searchWord' type="text" class="form-control" placeholder="Search Events..."
value= "<?php echo $searchWord;?>" ><br>
You missed echo and should write this echo "'".$searchWord ."'"; below the input tag.
Because the document.getElementById('filter-searchbar')... statement would executed successfully after only the dom is ready.
<h3> <div class="label label-default"> Search for Events </div></h3>
<BR>
<div id="filter" class="col-md-3">
<input id='filter-searchbar' name='searchWord' type="text" class="form-control" />
<?php
if(isset($_GET['s'])){
$searchWord = $_GET['s'];
?>
<script>
document.getElementById('filter-searchbar').value = <?php echo "'".$searchWord ."'"; ?> ;
</script>
<?php }
?>
document.getElementById('filter-searchbar').value = is missing an echo statement.
I modified the Searchform.php to:
'search_id' => 'id', 'form_action' => ( 'http://local.amleo.com/newps/pulldata.php'
For the first Search widget, to go to a custom PHP page that displays results for something else.
The next Search Widget, I'd like to search the category "AML Hot Topics". Not sure how I can do that. Any ideas??
So you can visualize: http://i.imgur.com/HSd9EEZ.png
The 1st Search is the one I modified the Searchform.php for. The 2nd is the one I'm not sure about.
I'm no super-duper PHP wizard by any means, but I can follow directions pretty decently.
You don't need two target for search, instead you can do it using something like this
One form for newps
<form method="get" id="searchform" action="<?php echo esc_url( home_url() ); ?>">
<input type="text" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" id="s" />
<input type="hidden" value="newps" name="key" />
<input type="submit" id="searchsubmit" value="<?php esc_attr_e('Search','Aggregate'); ?>" />
</form>
Another for AML
<form method="get" id="searchform" action="<?php echo esc_url( home_url() ); ?>">
<input type="text" value="<?php echo esc_attr( get_search_query() ); ?>" name="s" id="s" />
<input type="hidden" value="aml" name="key" />
<input type="submit" id="searchsubmit" value="<?php esc_attr_e('Search','Aggregate'); ?>" />
</form>
Create a search.php file in your theme's root folder, something like this
get_header();
// if you get `key` then it will be your custom search
// otherwise, default search will be performed
if(!empty($_GET['key'])) {
$key = $_GET['key']; // it will be either 'newps' or 'aml'
$search = $_GET['s'];
// modify the query using your $key and $search param
query_posts(...);
}
if (have_posts()) :
while(have_posts()): the_post();
// the_title() and more...
endwhile;
endif;
// reset the query if modified
if(!empty($key)) wp_reset_query();
get_sidebar();
get_footer();
I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.
The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed.
for example
<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
<?php
}
?>
<input type="submit" />
</form>
Any help is appreciated.
I think that what you need is:
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
$_POST[$col] //this will have the input value defined above.
In process.php you have to do the same query as mentioned above. If you iterate through those results $_POST[$col] will contain the posted values.
You need to do like this:
<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>
Now, mysql_fetch_assoc gets you the database row in a associative array. Then, the code iterates each column in the row and displays the name/value pair for it. And yes, you were not closing the value tag correctly.
foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}
I think that you want to change the name of the input to something that is constant.
For example:
<input type="text" name="testname" value="<?php echo $variable['val'] ? />
And then retrieve your variable like so:
$_POST['testname']
For example you could print the variable you sent in the input to test it like so:
echo $_POST['testname'];
You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />