Undefined index error while submitting form - php

I have used a script to hide an input when an option value is selected. The form gets submitted but gives me an undefined index error for the variable which i have hidden. My script is as follows:
function Sbox()
{
if(document.getElementById("selectdrop").value=='2')
{
document.getElementById("text2").disabled=true;
}
}
and my form is a s follows:
<form action="" method="POST>
<select name="text1" id="selectdrop" onchange="return Sbox();">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="text" id="text2" name="text2"/>
</form>
when I select option 2, textbox2 is disabled. But when I submit the form , my data is being inserted but is showing an error of undefined index for text2.
My PHP code for submit is as follows:
if(isset($_POST['submit']))
{
$text1=$_POST['text1'];
$text2=$_POST['text2'];
$sql="INSERT INTO letters (text1 , text2) VALUES ('$text1' , '$text2')";
if(mysqli_query($connect , $sql)==true){
echo "data has been successfully entered";
}
else{
echo "data not entered";
}
}

You are disabling text2. Try using following code and it should work fine.
function Sbox()
{
if(document.getElementById("selectdrop").value=='2')
{
var s = document.getElementById("text2");
s.setAttribute("type", "hidden");
}
}
Now you will be able to get the value of text2 in PHP as its hidden and not disabled.Disabled element's value does not get POSTED.
Hope it helps.
Thanks.

Related

How to fetch value of a drop-down and a text box after btn click?

I have a form wherein I have a drop down which is being populated from the database also I have an input box right beneath it. I want to fetch the value of both the fields via Ajax using jQuery and insert them into the database.
Problem: Value of the text field is getting inserted successfully but the value of drop down is not getting inserted.
I know I will have to fetch the value of the drop down separately and then add it to the data in ajax but I am not able to figure how to do the same.
NOTE: COULD THE DOWN VOTERS BE KIND ENOUGH TO TELL ME WHY I WAS DOWNVOTED SO THAT I COULD IMPROVE UPON THINGS.
sub_category_add.php
<form method="post">
<select id="cat_sub_cat">
<?php
$data=mysqli_query($con,"SELECT * FROM category");
while($row=mysqli_fetch_array($data))
{
echo "<option value=".$row['cid'].">".$row['category']."</option>";
}
?>
</select><br><br>
<h3>Add Sub Categories</h3><br>
<input type="text" name="sub_cat"><br><br>
<input type="submit" name="submit" value="Insert" id="sub_cat_btn">
</form>
Ajax File:
$(document).on('click','#sub_cat_btn', function(event){
event.preventDefault();
$.ajax({
url:"sub_cat_add_back.php",
method:"post",
data:$('form').serialize(),
dataType:"html",
success:function(strMsg){
$("#cat_sub_msg").html(strMsg);
}
})
sub_cat_add_back.php
<?php
include "../includes/config.php";
$name=$_POST['sub_cat'];
$cid=$_POST['cat_sub_cat'];
$data=mysqli_query($con,"INSERT INTO subcategory (name,cid) VALUES ('$name','$cid')");
if($data=="true")
{
echo "Successfully Inserted";
}
else
{
echo "Error";
}
?>
Your problem is that the select tag does not have the Name attribute
<select id="cat_sub_cat" name="cat_sub_cat">
If you want to get values on click instead of form serialize. add id in input
<input type="text" name="sub_cat" id="custom">
$("#cat_sub_cat option:selected");
$("#custom").val();
and if you want to do that with form serialize then add name in your select
<select id="cat_sub_cat" name="your_select_name">

Posting corresponding datas on clicking submit button

for($i=0;$i<2;$i++)
{
?>
<tr>
<td>
<select name="sel_<php echo $i; ?>" id="sel_<php echo $i; ?>">
<option value="1">A</option>
<option value="2">B</option>
</select>
</td>
<td>
<input type="submit" name="sub_<?php echo $i; ?>" value="submit" />
</td>
</tr>
<?php
}
?>
Now I have 2 select boxes sel_1 and sel_2, each of them have corresponding submit button sub_1 and sub_2. I want to post the corresponding data when a submit button is pressed. For example: when I press sub_1 I need to post the value of sel_1.
So, if press on the submit button in a specific row, I need to post the value of the select box in that row.
How can I do this?
It doesn't matter that both select are sent. Just work with the one select you need.
if (isset($_POST['sub_0'])) {
$data = $_POST['sel_0'];
} elseif (isset($_POST['sub_1'])) {
$data = $_POST['sel_1'];
}
In $data will be value of the first, or the second one, select.
IF you need to send just one selectbox, you need more forms (each form will contain select and submit).
since you have not mentioned the form method i am using $_REQUEST here,
if (isset($_REQUEST['sub_0'])) {
// use the value of $_REQUEST['sel_0'];
} elseif (isset($_REQUEST['sub_1'])) {
// use the value of $_REQUEST['sel_1'];
}
Note: you can replace $_REQUEST with $_POST or $_GET as per you form method type or else you can use $_REQUEST itself if you wish to do so

Send all values from a combobox to an array variable through post method

I want to be able to send all values from a combobox inside a form, into a php $_POST variable.
NOTICE: I do not need to send the selected values but everything contaning multiple select.
My attemp was this (it's not the right way but I think it's close):
HTML code:
<form action="" method="post" name="form" id="list2">
<select multiple size="8" style="width:280">
<option value="aline.chastel" name="user[]">Aline Chastel</option>
<option value="bruno.freitas" name="user[]">Bruno Sousa Freitas</option>
</select>
<input type="submit" name="submit" value="Send" />
</form>
PHP code:
if(isset($_POST['submit'])){
echo $_POST['user'][0].' '.$_POST['user'][1];
}
Expected outcome: aline.chastel bruno.freitas
My workaround:
After research I found out it wasn't possible to do this with pure HTML, so I had to use javascript and AJAX. I took all children elements and joined them in one string with '|'s:
$("#btngrafico").click(function() {
for(i=0;i<$("#list2").children().length;i++){
var co_usuario = $("#list2").children('option').eq(i).attr('value');
var usuario = $("#list2").children('option').eq(i).text();
if (i==0){
arr_co_usuario = co_usuario;
}else{
arr_co_usuario = arr_co_usuario + '|' + co_usuario;
}
}
$.ajax({
url: 'GraficoBarras.php',
type:'POST',
data: { 'arr_co_usuario': arr_co_usuario,'Month1':Month1,'Year1':Year1,'Month2':Month2,'Year2':Year2},
beforeSend: function(){
$("#loader").show();
},
success: function(output_string){
$("#ListadoRelatorio").append(output_string);
}
});
});
you must use multi-select list , for this purpose , because select list only send one value which is selected.
if you only want to use this select list then i think you only can get all the options via jquery
your name attribute should be in select tag, like:
<select multiple size="8" style="width:280" name="user[]">
<option value="aline.chastel">Aline Chastel</option>
<option value="bruno.freitas">Bruno Sousa Freitas</option>
</select>
to see the data on PHP side on form submission, do:
print_r($_POST["user"]);
if (isset($_POST['user'])) {
foreach ($_POST['user'] as $name) {
echo $name."<br />";
}
}

check if post request has been recieved

i have created html form (in MyTestFile.php file) with several options, and what i want to do is to check if post request has been recievied. Or in other words when user chooses something from my combobox and pushes button "Go" then it goes to the same page and sends choosed size value and echoes the size, and then form is not visible any more when size has been set. So here is my form code which isn't really working:
if (isset($_POST('submit'))) {
?>
<html>
<h2>Select size:</h2>
<form action="MyTestFile.php" method="POST">
<div>
<select name="SIZE">
<option value="big">big</option>
<option value="medium">medium</option>
<option value="small">small</option>
</select>
<input type="submit" value="Go">
</form>
</html>
<?php
} else {
echo $_POST('submit');
//and do some stuff here
}
The main idea: check if post request with size has been recieved, if not - show the form, where user can choose size.
$_POST is an array, so you must use square brackets to access the elements, not parenthesies. So change this:
if (isset($_POST['submit'])) {
// ^ ^ square breackets not ()
Secondly your submit button doesn't have a name, so your isset() check will never be true. Give it a name:
<input type="submit" name="submit" value="Go">
Or use the SIZE select in the isset check:
if (isset($_POST['SIZE'])) {
Finally your isset check needs reversing, in order to show the form when the post value is not present:
if (!isset($_POST['SIZE'])) {
// ^ not operator to reverse the check
I can see you tried so here you go:
Just add an exclamation mark before isset, add a name to the submit button and replace those parentheses with square brackets.
if (!isset($_POST['submit']))
{
?>
<html>
<h2>Select size:</h2>
<form action="MyTestFile.php" method="POST"><div>
<select name="SIZE">
<option value="big">big</option>
<option value="medium">medium</option>
<option value="small">small</option>
</select>
<input type="submit" name="submit" value="Go">
</form>
</html>
<?php
}
else
{
echo $_POST['size'];
//and do some stuff here
}
Use [ don't use ( in $_POST method. Because $_POST is array so use square brackets. Use same submit as name in submit button.
$_POST['submit'] instead of $_POST('submit')
and add ! (Not Set) in if condition, use below code,
if(!isset($_POST['submit'])){
// Your Form Code
}
else{
print_r($_POST);
}

How to have a select box in form populate a input box below using php

I have a requirement for adding values selected in a select box to a input box with the same form and am not sure how to best go about this. The select box contains user email addresses and the input box needs to contain multiple email addresses. The idea is that when you select a email address from the select box it is then added to the input either automatically, or by clicking the add button which reload the page in PHP adding to a variable that is the value of the input box. I have tried to do this by having a form within a form but that does not work is there a better way of doing this?
Many thanks.
Well, in the select's onchange event, set the inputs value to all of the selected options concatenated together.
Here's some psuedo-code:
on select changed {
str= ''
for every element
if checked
str = str + value + ','
set input's value to the variable str
Again in jQuery:
$('select').onchange(function(){
str='';
$('option:selected').each(function(){
str+=this.value+','; });
$('input:text').value(str); });
Edit: Here's what you wanted, without multi-select.
$('select').onchange(function(){
$('option:selected').each(function(){
$('input:text').val($('input:text').val()+$(this).val()+',');})
.removeAttr('selected'); });
You can try this:
Write this code in the top of the page before DOCTYPE.
<?php
if(isset($_POST['add']))
{
$previous_email = $_POST['txt_email'];
$emails = $_POST['select_email'].",".$previous_email;
}
else
{
$emails = '';
}
?>
The javascript: you can put it in the head section of your html document
<script>
function add_email()
{
document.getElementById('txt_email').value = document.getElementById('select_email').value +','+ document.getElementById('txt_email').value;
}
</script>
And finally the html:
<body>
<form method="post" action="" name="myform">
<select name="select_email" id="select_email" onchange="add_email();">
<option value="email1#test.com">email1#test.com</option>
<option value="email2#test.com">email2#test.com</option>
<option value="email3#test.com">email3#test.com</option>
</select>
<textarea name="txt_email" id="txt_email"><?=$emails;?></textarea>
<input type="submit" value="add" name="add"/>
</form>
</body>
Hope it helps

Categories