calling a php function in form - wordpress - php

im trying to call a php function in submiting a form.
here my full php file: ( am i write correct? )
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
?>
<div class="manual_sending_form" id="manual_sending_form">
<div class="inside">
<form class="initial-form" id="manual_sending" method="post" name="post"
action="<?php echo admin_url( 'admin.php?page=my-page' ) ?>">
<p>
<label for="resive_number">My Field</label><br>
<input type="text" name="resive_number" id="resive_number"
value="<?php echo isset( $_POST['sms_numbers'] ) ? sanitize_text_field( $_POST['sms_numbers'] ) : '' ?>"
</p>
<p>
<input type="submit" class="button button-primary" name="send_sms_button"
value="Send">
</p>
</form>
</div>
</div>
<?php
if ( isset( $_POST['send_sms_button'] ) ) {
try
{
$phone = $_POST['sms_numbers'];
// do some code hwre
?>
did i correct on below line? i just want to input something there ( i need this value on function.
value="<?php echo isset( $_POST['sms_numbers'] ) ? sanitize_text_field( $_POST['sms_numbers'] ) : '' ?>"
or this part is correct ?
( $phone = $_POST['sms_numbers']; )
i dont know why i get below error, ////
Notice: Undefined index: sms_numbers in

Try to replace your code into this
<input type="text" name="sms_numbers" // change input name to sms_numbers
and set this line at the top of your page
ini_set('display_errors',0);

Related

Wordpress in foreach loop updating meta values

I have done foreach loop to get custom post type to frontend. And i have one custom field called 'order_staatus'. When i look in front end my list in loop, i want to add one button what would change this certain post 'order_staatus' to different value.. my code..
<? foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div class="profile_order_row">
<?php the_author(); ?> <?php the_title(); ?>
<?php
Global $post;
if ( isset( $_POST['submit'] ) )
{
if( ! isset( $post ) ) {
echo 'Post not set';
die();
}
else if( ! isset( $_POST['staatus'] ) && ! empty( $_POST['staatus'] ) ){
echo 'Error';
die();
}
$postid = $_POST['post_id'];
update_post_meta($postid,'order_staatus','1');
}
$staatus = get_post_meta($post->ID, 'order_staatus', true);
echo print_r($staatus);
?>
<form method="post" action="">
<input type="hidden" name="post_id" value="'.$post->ID.'" />
<input type='text' name='staatus' value='<? echo $staatus ?>' />
<input type='submit' value='save' />
</form>
</div>
<?php endforeach; wp_reset_postdata(); ?>
I don't think the issue is with the function update_post_meta. I think the more likely issue is that $_POST['submit'] is not set. When submitting a form, the value of the submit button is not sent. The value attribute is simply for assigning the text of the button.
I would rewrite the IF block of code like this:
if ( isset( $_POST ) )
{
if( ! isset( $_POST['staatus'] ) && ! empty( $_POST['staatus'] ) ){
echo 'Error';
die();
}
$postid = sanitize_text_field($_POST['post_id']);
update_post_meta($postid,'order_staatus','1');
}
Note, that I removed the isset($post) check because you are running this code inside of foreach ( $postslist as $post ) which defines $post so it will always be set within that loop.
I also added the function sanitize_text_field() to sanitize the post_id. This is an important security measure to avoid SQL injection. All user input, including $_POST, can contain dangerous data and needs to be sanitized before use.
Hope this helps!

Page not loading when trying to handle forms

I'm new to PHP and I'm trying to handle a form so I added a if condition to check if the submit button was pressed and if the other elements in the form are set. For some reason it causes the page to not load (it loads, but shows a blank page) so I can't actually submit anything..
When I tried removing the php code, it loaded fine. What am I doing wrong here?
<body>
<?php
if(isset(filter_input(INPUT_POST, 'aglogin')) && isset(filter_input(INPUT_POST, 'agname')) && isset(filter_input(INPUT_POST, 'agpass')))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
</body>
Do not use isset(); because filter_input() returns true if variable is set and return false if it is not set.
<body>
<?php
if(filter_input(INPUT_POST, 'aglogin') && filter_input(INPUT_POST, 'agname') && filter_input(INPUT_POST, 'agpass'))
{
echo 'Submitted..';
}
?>
<form method="post">
Username: <input type="text" id="agname" name="agname"/>
<br>
Password: <input type="password" id="agpass" name="agpass"/>
<br>
<input type="submit" id="aglogin" name="aglogin" value="Login">
</form>
You could try this
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$valid=( array_key_exists('aglogin',$_POST ) && array_key_exists('agname',$_POST ) && array_key_exists('agpass',$_POST ) ) ? true : false;
if( $valid ) echo "Form submitted successfully";
}
?>

If the value exists show in wordpress admin (Form's have been created on the fly so number increments)

I'm creating my first options page and i'm trying to upload images for a slider, I don't know how many images are going to be in the slider so I will need to add more with a + button, It will automatically show one text input an upload button and a + button to start off with, should I require more i'll click the + button which will then add another text input, upload button a + button and a - button.
I've got this working to a point, it still needs a little help but it's getting there http://jsfiddle.net/vs8p5/5/
So far if I upload an image and click the + button, it will give me the option to upload another, this is working great and the images upload to wordpress.
Now for the issue.
I'm using this part of code to retrieve the data from wordpress
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
echo "<script type='text/javascript'>alert('yes it has');</script>";
}
This tells me that if there is a value then echo the alert
Now, working form that code above I've
function kandibox_hero_upload_image_link_callback($args) {
$hero_options = get_option( 'hero_options' ); ?>
<div id="upload_image_sets"> <?php
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}
else { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div>
<?php } ?>
</div> <?php
}
What i'm trying to do here is echo a form with a value, if nothing exists, echo a blank form, this seems to work although I don't think it's correct.
The main issue is that it will only echo the first form, when you click the + buttons, the form's id, name, and value script increments.
So now the question.
How can I get the form to look for all the $hero_options['upload_image_link_...'] and echo out a form for each if it exists? then echo out 1 blank form if nothing exists?
I'm pretty sure i've covered everything you might need but if i've missed something, let me know and i'll add it.
I've followed about 20 tutorials old and new and come up with this.
To add the information to the wordpress database, i'm using the following code.
function register_hero_options() {
add_settings_section(
'hero_settings_section', // ID used to identify this section and with which to register options
__( 'hero Options', 'kandibox' ), // Title to be displayed on the administration page
'kandibox_hero_options_callback', // Callback used to render the description of the section
'hero_options' // Page on which to add this section of options
);
add_settings_field(
'show_hero_options', // ID used to identify the field throughout the theme
__( 'hero', 'kandibox' ), // The label to the left of the option interface element
'kandibox_toggle_hero_callback', // The name of the function responsible for rendering the option interface
'hero_options', // The page on which this option will be displayed
'hero_settings_section' // The name of the section to which this field belongs
);
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['show_hero_options'] ) && $hero_options[ 'show_hero_options' ] ) {
add_settings_field(
'hero_size',
__( 'Size', 'kandibox' ),
'kandibox_hero_size_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'hero_background',
__( 'Background', 'kandibox' ),
'kandibox_hero_background_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'upload_image_links',
__( 'Upload Image', 'kandibox' ),
'kandibox_hero_upload_image_link_callback',
'hero_options',
'hero_settings_section'
);
}
register_setting(
'hero_options',
'hero_options'
);
}
add_action( 'admin_init', 'register_hero_options' );
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
ok i juss wrote a rough code for you might this will help you. this is according to the array you have go. there might be some better ways of doing it.
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
</div> <?php
Updated the code so now it's correct.

wordpress backend checkbox won't save the value (checked)

I working on a theme options page for my template and want to integrate a checkbox. The problem is, the checkbox won't save the value. Where is my mistake? Well, I'm clueless...
<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_init(){
register_setting( 'wordpress_options', 'wordpress_theme_options');
}
function theme_options_add_page() {
add_theme_page('Options', 'Options', 'edit_theme_options', 'theme-options', 'wordpress_theme_options_page' );
}
function wordpress_theme_options_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false; ?>
[...]
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade">
<p><strong>Settings saved!</strong></p>
</div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'wordpress_options' ); ?>
<?php $options = get_option( 'wordpress_theme_options' ); ?>
<div id="admininterface">
[...]
<p>
<p class="before-form">Show RSS Icon:</p>
<?php $options = get_option( 'icon-rss-show' ); ?>
<input type="checkbox" name="$options[icon-rss-show]" value="1"<?php checked( 1 == $options['icon-rss-show'] ); ?> />
</p>
[...]
</div><!-- #admininterface -->
</form>
</div>
<?php } ?>
Adding another hidden input field will actually make the form save the zero value in the database like this:
<form>
<input type='hidden' value='0' name='selfdestruct'>
<input type='checkbox' value='1' name='selfdestruct'>
</form>
Make sure to place the hidden input above your regular one, with the value '0' and the same name as in the regular input field.
That answer is from here: https://stackoverflow.com/a/1992745/4688612
Please send all credit to that poster.

Empty PHP POST Variables

Background
Web-based contact form.
Problem
The $_POST array is empty. When errors are enabled, no errors (except empty array values) are found. The code was tested and working at one point, then left untouched until I posted this question. The host might have performed an upgrade.
Software
PHP 5.2.17
Apache 2.0.63
SSI
HTML Form
The HTML form is as follows:
<form method="post" action="contact.php" id="commentForm">
<label for="name">Name</label>
<input type="text" name="name" id="name" maxlength="64" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" maxlength="320" /><br />
<label for="message">Message</label>
<textarea name="message" rows="10" cols="40" id="Message"></textarea><br />
<label for="human">40 + 2 =</label>
<input type="text" name="human" id="human" size="10" maxlength="3" /><br />
<p align="center">
<input type="submit" name="submit" value="Send" class="submit-button" />
</p>
</form>
PHP Code
The following code is called when the form is submitted:
$reason = 'default';
error_reporting( 0 );
ini_set( 'display_errors', 0 );
ini_set( 'register_globals', 0 );
ini_set( 'allow_url_fopen', 0 );
ini_set( 'expose_php', 0 );
ini_set( 'magic_quotes_gpc', 0 );
function not_contacted() {
global $reason;
// Redirects to computer, name, email, or message.
//
header( 'Location: ../error-'.$reason.'.shtml' );
}
function wms_error_handler($errno, $errstr, $errfile, $errline) {
not_contacted();
return true;
}
function wms_shutdown() {
if( is_null( $e = error_get_last() ) === false ) {
not_contacted();
}
}
set_error_handler( "wms_error_handler" );
register_shutdown_function( 'wms_shutdown' );
$name = trim( stripslashes( $_POST["name"] ) );
Logging
echo $_SERVER["REQUEST_METHOD"]; == GET
print_r( $_GET ); == Array ( )
print_r( $_POST ); == Array ( )
print_r( $_REQUEST ); ==
Array ( [__utma] => 181723617.1357984856.1311884601.1313715852.1313720411.12 [__utmz] => 181723617.1313720411.12.10.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=jigo [__utmc] => 181723617 [__utmb] => 181723617.3.10.1313720411 ) `
file_get_contents('php://input') == Empty
Questions
What could be causing the PHP POST variable values to be empty?
Why is the POST method being transmogrified into a GET method?
I think it is a php.ini or httpd.conf conflict, but cannot be sure (it is a hosted domain).
Thank you.
Update
The following test works.
test.shtml
<html>
<body>
<form method="post" action="test.php">
<input type="hidden" name="test" value="test" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
test.php
<?
echo $_POST["test"];
?>
I also had a similar issue.
The problem was the data were posted to the http url, but there was a redirection to the https version of the url. Datas in $_POST are lost during the redirect.
I hope this can help others
Remove the following line from the .htaccess file:
RewriteRule ^(.*)$ http://www.whitemagicsoftware.com/$1 [R=301,L]

Categories