Passing form selected data to controller in laravel7, returns null - laravel-blade

I need to pass selectbox selected data to Laravel Controller.
But It always return null.
What is wrong in my code, Thanks You.
<td class="mr-3">
<select class="form-control" name="section[]" id="section" value="">
<option>Select Section</option>
#foreach ($sections as $key => $value) {{ $value }}
<option value="{{ $key }}" {{ ( $key == $selectedID) ?
'selected' : '' }}>
{{ $value }}
</option>
#endforeach
</select>
</td>
In my Controller:
public function store(Request $request)
{
foreach($request->input('sections') as $key => $value){
$data['value'] = $key;
}
dd ($data['value']); // <- Returns Null !

use the name with out []
<select class="form-control" name="section" id="section" >
in the controller
$data['value'] = $request->input('sections');
if you want multiple selection
<select multiple="multiple" name="sections[]" id="sections">
in the controller
foreach($request->sections as $key ){
$data['value'] []= $key;
}

Related

Wordpress Form Shortcode

I have created a shortcode for a form in WP. Now I need to make it dynamic and add <select> tag with <options> for posts and taxonomies.
function shortcode_form(){
$form = "<form method='post'>
<select name='escape-date' id='escape-date' required>
/* NEED TO ADD FUNCTION AND LOGIC TO DISPLAY POSTS */
</select>
<select name='escape-date' id='escape-date' required>
/* NEED TO ADD FUNCTION AND LOGIC TO DISPLAY TAXONOMIES ASSOCIATED WITH SELECTED POST ABOVE */
</select>
</form>";
return $form;
}
There are two issues:
1 - $form is a string so I cannot add functions inside it. Which options do I have?
2 - Change the second <select>. Would this only be possible with JS?
You can populate <select> with a list of posts using something like this:
/** #var wpdb $wpdb */
global $wpdb;
$posts = query_posts(['post_type' => 'post', 'nopaging' => true]);
$posts_options_tags = array_map( function ( $post ) {
return "<option value='{$post->ID}'>{$post->post_title}</option>";
}, $posts );
$posts_options_tags = implode('', $posts_options_tags);
$form = "<form method='post'>
<select name='post_id' id='post_id' required>
<option>Select a post</option>
$posts_options_tags
</select>
<select name='escape-date' id='escape-date' required>
/* NEED TO ADD FUNCTION AND LOGIC TO DISPLAY TAXONOMIES ASSOCIATED WITH SELECTED POST ABOVE */
</select>
</form>";

WordPress Contact Form 7 (CF7) plugin how to add data-attributes to select field options

I have a custom CF/ select field: [select* pcname]
I use this function to add the values to the field:
function thebox_add_cf7_select_product_list( $tag, $unused ) {
if ( $tag['name'] != 'pcname' ) {
return $tag;
}
$args = array ( 'post_type' => 'my_products',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);
$products = get_posts($args);
if ( ! $products )
return $tag;
foreach ( $products as $product ) {
$tag['raw_values'][] = $product->post_title;
$tag['values'][] = $product->post_title;
$tag['labels'][] = $product->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'thebox_add_cf7_select_product_list', 10, 2);
This works great. I get something like this as my output:
<select name="pcname" class="wpcf7-form-control wpcf7-select"">
<option value="Product A">Product A</option>
<option value="Product B">Product B</option>
<option value="Product C">Product C</option>
....
</select>
But now I need to add additional Informations to each option like the Product-ID and the Product-Main-Category. Therefore I want to use data-attributes, but I do not know how to add them in my wpcf7_form_tag function above? How is it possible to add data-attributes to the options of my select-field?
The output should look like this:
<select name="pcname" class="wpcf7-form-control wpcf7-select"">
<option value="Product A" data-id="1" data-cat="Category A">Product A</option>
<option value="Product B" data-id="2" data-cat="Category B">Product B</option>
<option value="Product C" data-id="3" data-cat="Category B">Product C</option>
....
</select>
Try with this CF7 filter code in functions.php
add_filter( 'wpcf7_form_elements', 'imp_wpcf7_form_elements' );
function imp_wpcf7_form_elements( $content ) {
$str_pos = strpos( $content, 'name="unique-name"' ); //name='yourfield name'
if ( $str_pos !== false ) {
$content = substr_replace( $content, ' data-attr="custom" data-msg="Foo Bar 1" ', $str_pos, 0 );
}
return $content;
}

Wordpress posts order by "Searched term" and by "post rating"

So on my websites sidebar I want to put small filter form that will allow to filter posts by specific search terms and sort them by rating.
So for search part I use this workaround. Which is not perfect but works fine. It's just regular search:
<form method="get" id="searchform" action="">
<div>
<label for="s"></label><br/>
<select value="" name="s" id="s" >
<option value="town1">town1</option>
<option value="town2">town2</option>
<option value="town4">town4</option>
<option value="town5">town5</option>
</select>
<select name="test1">
<option value="ASC">ASC</option>
<option value="DSC">DSC</option>
</select>
<input onclick="myfunction()" type="submit" id="searchsubmit" value="Find" />
</div>
</form>
But I have issue with ASC/DSC sorting by rating. The idea is that in each post I have different numeric value (raiting) that is assign to <p> element
For example:
post1: <p id="test">5.5<p>
post2: <p id="test">0.5<p>
post3: <p id="test">3.5<p>
post4: <p id="test">1.5<p>
So I want to give user ability to search posts by town name (this function works already) and sort by raiting (ASC or DSC), and want to not just echo post title lists but show posts in regular manner (like: title, excerpts etc.)
So far sorting by raiting is messing everything, because I have no idea how to do that if I have to sort it by <p> IDs and how to connect this 2 rules: search and sorting.
Myfunction code:
<?php
function myfunction( ) {
$args = array(
'orderby' => jQuery('#test'),
'order' => 'DESC',
);
$query = new WP_Query( $args );
}
?>
Obviously jQuery('#test') is not going to work like that in PHP. Also using onclick="myfunction()" to call a PHP function is impossible. JS is executed on the client machine and PHP is executed on the server. This would never work.
Calling a PHP function with JS is impossible, unless you use AJAX.
<body>
<form method="get" id="searchform" action="">
<div>
<label for="s"></label><br/>
<select name="s" id="s">
<option value="town1">town1</option>
<!------------ -->
</select>
<select name="test1">
<option value="ASC">ASC</option>
<option value="DSC">DSC</option>
</select>
<input type="submit" id="searchsubmit" value="Find"/>
</div>
</form>
<div id="posts"></div>
<script>
jQuery( document ).ready( function ( $ ) {
$( "#searchsubmit" ).on( "click", function ( e ) {
$.ajax( {
type: "POST",
url: "/admin_ajax.php",
data: {
action: "my_action",
orderby: $( "#s" ).val(),
order: $( "#test1" ).val()
},
success: function ( result ) {
$( "#posts" ).html( result );
}
} );
e.preventDefault();
} );
} );
</script>
</body>
PHP:
<?php
add_action('wp_ajax_my_action', 'myPHPFunction');
add_action('wp_ajax_nopriv_my_action', 'myPHPFunction');
function myPHPFunction()
{
if (!isset($_POST['orderby']) || !isset($_POST['order'])) {
return;
}
$args = array(
'orderby' => $_POST['orderby'],
'order' => $_POST['order'],
);
$query = new WP_Query( $args );
// loop through your posts and echo the templates
// stop execution
wp_die();
}

How to add select html to my own contact form "In Wordpress"

I created my own contact form wordpress. I can send "name" "e-mail" .. But i can't send html select info and checkbox value. Can you help me please?
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="checkbox" name="vehicle" value="Car">I have a car
And using this php:
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
$to = get_option('admin_email');
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
You need to give your select input a name.
for example:
<label for='vehicle-brand'>Select a vehicle brand:</label><br>
<select name="vehicle-brand">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
the code for your checkbox looks good. To learn more about using checkboxes in PHP forms try this link: http://form.guide/php-form/php-form-checkbox.html
<input type="checkbox" name="vehicle" value="Car">I have a car
Then to post this info.
$vehiclebrand = sanitize_text_field( $_POST["vehicle-brand"] );
$vehicle = ( isset( $_POST["vehicle"] ) && $_POST['vehicle'] == 'Car' ? esc_attr( $_POST["vehicle"] ) : '' );
Then you need to add these variables in your message to receive them.
It is a good idea to sanitize your variables for security of your WordPress website. The sanitize_text_field() function is a WordPess function, you can read more about it here: https://developer.wordpress.org/reference/functions/sanitize_text_field/
I hope that helps.

getting post data in a plugin

hey guys when i am writing my plugin how do i get the POST data from the textbox and insert it into the db.
I tried but i am only able to create a table.
Thanks
Assume you have a form like the one below
<form name="form1" method="post" action="">
<p>First Name:
<input type="text" name="first_name" size="20">
</p>
<p>Last Name:
<input type="text" name="last_name" size="20">
</p>
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="Create" />
</p>
</form>
Then to get the posted data you would write something like the one below.
<?php
if( isset($_POST['Submit']) && $_POST['Submit'] == 'Create' ) {
//Reads the posted values
$first_name = $_POST[ "first_name" ];
$last_name = $_POST[ "last_name" ];
global $wpdb;
$table_name = $wpdb->prefix . "names";
$rows_affected = $wpdb->insert( $table_name, array( 'f_name' => $first_name, 'l_name' => $last_name) );
}
?>
Depending on the information your plugin needs, the structure of the form and inserting it into the database will vary.

Resources