0
  1. Input field inside a while condition i'm displaying the tab in which tab values are fetched from table red_digid_info

    <form method="POST" action="" id="form_isp_status" onsubmit="submit_isp_status('form_isp_status');"">
                                <div class="row">
                                <label for="fromdate" class="col-sm-1 control-label">&nbsp;&nbsp;FROM DATE </label>
                                <label for="todate" class="col-sm-1 control-label" style="margin-left: 4cm;">&nbsp;&nbsp;TO DATE </label>
                                </div>
                                <div class="row">
                                <div class="col-sm-1">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="fromdatepicker" name="fromdate" placeholder="yyyy-mm-dd"  style="width:200px;height:33px;"> 
                                    <span class="input-group-addon"><i class='glyphicon glyphicon-calendar'></i></span>
                                </div>
                                </div> 
                                <div class="col-sm-1" style="margin-left:4cm">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="todatepicker" name="todate" placeholder="yyyy-mm-dd"  style="width:200px;height:33px;"> 
                                    <span class="input-group-addon"><i class='glyphicon glyphicon-calendar'></i></span>
                                </div>
                                </div>
                                <div class="col-sm-offset-2 col-sm-2">
                                        <input type="submit" value="ISP Status" class='btn btn-purple btn-rounded w-md  m-b-5' name="isp_button">
                                        <input type="hidden" value="1" name="pointer">
                                        <button type="button" class="btn btn-pink btn-rounded w-md  m-b-5" onclick="resetforms('form_isp_status')">Reset</button>
                                </div>
                                </div>
                                <div class="row">
                                <label for="isp" class="col-sm-1 control-label">&nbsp;&nbsp;SELECT ISP</label>
                                </div><div class="row">
        <div class="tab">
            <?php 
                $isp_tab = mysql_query("SELECT DISTINCT(`isp`) FROM `red_dgid_info`");
                while ($result = mysql_fetch_array($isp_tab)) {
                    $isp_value = $result[0];
                    echo '<input class="tablinks ion-social-rss" type="submit" name="isp_value[]" value="'.$isp_value.'">';
                    //echo '<input type="hidden" name="isp_hidden_value[]" value="'.$isp_value.'">';
                } 
            ?>
        </div>
    </div></form>
    
  2. if i click any one value of a tab i ve to display the tab content so i need the value of submit button in php post method

    if($_REQUEST['pointer'] ==1)
    {   
    var_dump($_POST);                   
    //-------status criteria given---------------------// 
    
        //-----------isp tab submiited--------------// 
        if(isset($_POST['isp_value'])) 
        {
                print_r($_POST['isp_value']);
                $isp=$_POST['isp_value'];
    
        }
        //------------------end----------------------//
    
        //----------hidden value array--------------//
        /*$data = $_POST['isp_hidden_value'];
        foreach($data as $isp)
        {
            echo "isp_hidden =".$isp;
        }
        //---------------another way----------------//
        $isp_hidden = $_POST['isp_hidden_value'][$isp];*/
        //--------------end------------------------//
    
    $date= date("Y-m-d");;
    $fromdatepicker =$_POST['fromdate'];
    $todatepicker =$_POST['todate'];
    exit;
    }
    

if(isset($_POST['isp_value'])) //this if condition fails isp_value is not set don't know the reason and solution for it

  1. submit function

    function submit_isp_status(formId) { 
    if($("#"+formId).valid() == true) {
       $.ajax({
            type: 'POST',
            url: 'webxstatus.php', //same page
            data: $("#"+formId).serialize(), 
            success: function(data) {
                ..........
                                    }
            });
        }
    }
    

I'm stuck with this for past 2 days anyone help me to solve this.

Puvi
  • 41
  • 2
  • 12
  • 1
    it's because of your `name`. You have one name used in your loop so what will happen there is that as you've described only the latest value will be fetched. To fix this you try changing your name into an array something like this `name="hidden_value[]"` refer [here](https://stackoverflow.com/questions/4688880/html-element-array-name-something-or-name-something) – hungrykoala May 24 '17 at 05:13
  • In echo i got like this isp_hidden =Array if i use the name as name="hidden_value[]" @hungrykoala – Puvi May 24 '17 at 05:37
  • what does `submit_isp_status` do? – Jelmergu May 24 '17 at 06:34
  • It just submit the form @Jelmergu – Puvi May 24 '17 at 07:30

5 Answers5

0
     <?php $isp_tab=mysql_query("select distinct(isp) from red_dgid_info");
            while($result=mysql_fetch_array($isp_tab))
                {
echo'<form method="POST" action="" id="form_isp_status" onsubmit="submit_isp_status('form_isp_status');">';
            $isp_value =$result[0];
            echo "<input class='tablinks ion-radio-waves' type='submit' name='isp_value' value='$isp_value'></input>";
            echo '<input type="hidden" name="isp_hidden_value" value='$isp_value'>';
echo'</form>';
                }?>

put form inside while loop

  • Why put a form inside while loop?? If a table has 1000 records then it will generate 1000 form ... Is it valid?? and how to fetch data for individual form??? – Nidhi May 24 '17 at 05:17
  • its valid and which submit button you click only that form data will be send – Nishit Manjarawala May 24 '17 at 05:23
  • Are you talking about something like that http://phpfiddle.org/main/code/8z15-gm94 – Nidhi May 24 '17 at 05:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144985/discussion-between-nidhi-and-nishit-manjarawala). – Nidhi May 24 '17 at 05:38
  • I also have other fields like date if i use the form inside a while loop all the other fields are also repeated @NishitManjarawala – Puvi May 24 '17 at 05:44
  • yes it repeated but when you use form in loop and u click on submit only that form data will be submit – Nishit Manjarawala May 24 '17 at 05:46
  • wait i explain you here – Nishit Manjarawala May 24 '17 at 05:47
  • example. while(true){
    } for example in upon example if you got two form like below
    //if you click on this submit then it will send all data of this form only it means this form text box and submit value only
    – Nishit Manjarawala May 24 '17 at 05:50
0
 while($result=mysql_fetch_array($isp_tab))
            {
        $isp_value =$result[0];
        echo "<input class='tablinks ion-radio-waves' type='submit' name='isp_value' value='$isp_value'></input>";
        echo '<input type="hidden" name="isp_hidden_value[]" value='$isp_value'>';
            }?>


$data =$_POST['isp_hidden_value'];
    foreach($data as $isp)
    {
        echo "isp_hidden"=$isp;
    }

Use input field 'isp_hidden_value' as array and fetch values using foreach

  • by using for each i got all the values of a row but i only need a submmited tab value @sreeja seguro – Puvi May 24 '17 at 05:54
  • echo ""; use submit input text as array and get submitted tab value by $ispvalue=$_POST['isp_value']; $isp=$_POST['isp_value'][0]; – sreeja seguro May 24 '17 at 06:28
  • I have changed the name as name='isp_value[]' then how to get the submitted tab value can u explain me clearly @sreeja seguro – Puvi May 24 '17 at 07:05
  • when we submit tab value evenif it is array it pass only current tab value .ie $_POST['isp_value'] is an array containing only one value ie current tab value we get that value by '$_POST['isp_value'][0] 'this. – sreeja seguro May 24 '17 at 07:13
  • I have tried $_POST['isp_value'][0] still didn't get any value I think my isp_value is not set thats why i didn't get any submitted tab value @sreeja seguro – Puvi May 24 '17 at 07:27
  • first you try to set static value for isp_value inside the loop and check value for $_POST['isp_value'][0] – sreeja seguro May 24 '17 at 07:35
  • how to set the static value @sreeja seguro – Puvi May 24 '17 at 07:39
0

you need to change your input name to array like this isp_value[] so only you can get the value which submit button you clicked otherwise you will get last value only .

echo "<input class='tablinks ion-radio-waves' type='submit' name='isp_value[]' value='$isp_value'></input>"

PHP :

print_r($_POST['isp_value']);

And also minor single quotes problem in your isp_hidden_value

echo '<input type="hidden" name="isp_hidden_value[]" value="'.$isp_value.'">';

note:

if you need currently clicked submit button value means . don't use hidden field it will collect all values . just insert the value in submit button it will collect only curently clicked element value only as a array

Try this example :

<?php

if(isset($_POST['xyz']))
{

    print_r($_POST);
}
?>

 <form action="" method="post">

   <input type="submit" name="xyz[]" value="1" >
   <input type="submit" name="xyz[]" value="2" >

  </form>
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • In echo i didn't get any value isp = ' ' @JYoThI – Puvi May 24 '17 at 05:42
  • print_r($_POST['isp_value']); @puvi – JYoThI May 24 '17 at 05:45
  • and also minor single quotes problem in your isp_hidden_value echo ''; – JYoThI May 24 '17 at 05:49
  • if you need currently clicked submit button value means . don't use hidden field it will collect all values . just insert the value in submit button it will collect only curently clicked element value only as array @puvi – JYoThI May 24 '17 at 05:57
  • In print_r also i didn't get any value for isp_value @JYoThl – Puvi May 24 '17 at 06:02
  • what you get when you print ? is there any empty array ? @puvi – JYoThI May 24 '17 at 06:03
  • no I got nothing I think isp_value is not set dont know what's happening @JYoThl – Puvi May 24 '17 at 06:08
  • you need to change your name attribute to array like this name='isp_value[]' @puvi – JYoThI May 24 '17 at 06:27
  • i forget to update that in my code i ve changed it to isp_value[] and tried @JYoThl – Puvi May 24 '17 at 06:33
  • you can't echo the array use print_r($isp); @puvi – JYoThI May 24 '17 at 06:34
  • first of all you not setting value propel to the form . $isp_value = $result[0]; is not valid try this one $isp_value = $result[DISTINCT(`isp`)]; @puvi – JYoThI May 24 '17 at 06:38
  • yes i ve tried print_r($isp); but i get the submittted value through javascript alert but not in php @JYoThl – Puvi May 24 '17 at 06:39
  • by using $result[isp]; i got the same result by using $result[[DISTINCT(isp)] I dont even get the tab values @JYoThl Thank you so much for your kind replies :) – Puvi May 24 '17 at 06:58
  • Bit of a long shot but will try anyway, this answered a problem I was having with this but was wondering how I can have the array value and have a submit face value just for appearence to the customer. As the button value just says the value but I want it to say "Add" – Steven Wilmot Mar 04 '22 at 14:27
0

You need to do some changes in your code, and after that i hope it will work perfectly:

  1. Change hidden field

from:

echo '<input type="hidden" name="isp_hidden_value" value="$isp_value">';

to:

echo "<input type='hidden' name='isp_hidden_value[$isp_value]' value='" . $isp_value . "'>";
  1. In the post method change value assignment of hidden field

from:

$isp_hidden = $_POST['isp_hidden_value'];

to:

$isp_hidden = $_POST['isp_hidden_value'][$isp];

Rest should work fine.

Logic behind this change is to use array when using same name for multiple input types. Here you are using a flat variable which will hold only one value, which will get assigned at the end. If you use array it will hold multiple values and allows you to get your desired result.

Pranit Jha
  • 153
  • 5
  • my name='isp_value[]' is not set.. :( Is there any solution for it @Pranit Jha – Puvi May 24 '17 at 07:36
  • To hold values of all the fields generated from PHP loop we need to create array of it. So, here you need to define name as array by using "[]" after name. – Pranit Jha May 24 '17 at 07:46
  • I have created the array name, but in post method if(isset($_POST['isp_value'])) this condition fails @Pranit Jha – Puvi May 24 '17 at 07:53
  • Share your current code, so that i can figure out the actual issue. – Pranit Jha May 24 '17 at 08:52
  • It is working. I used your code, and it is resulting the array. – Pranit Jha May 24 '17 at 09:20
  • Code:
    "A"), array("0" => "B"), array("0" => "C") ); foreach ($isp_tab as $result) { $isp_value = $result[0]; echo ""; } ?>
    if (isset($_POST['isp_value']) && !empty($_POST['isp_value'])) { $isp = $_POST['isp_value']; print_r($isp); }
    – Pranit Jha May 24 '17 at 09:22
  • Only part i have changed is, i'm using static array instead of db result. – Pranit Jha May 24 '17 at 09:23
  • if i use isp_value as select option and submit it using another button it works and shows the result, but if i use 'isp_value' as a tab and submit it 'isp_value' is not set @Pranit Jha – Puvi May 24 '17 at 10:16
0

You don't need a hidden field for this. A button with a name should send it's value.

<?php
var_dump($_POST);
?>
<form method="POST" action="">
<div class="row">
    <div class="tab">
        <input type="submit" name="button" value="test1">
        <input type="submit" name="button" value="test2">
    </div>
</div> </form>

Will tell me that $_POST['button'] is either test1 or test2

Which means that the following should work

<form method="POST" action="" id="form_isp_status" onsubmit="submit_isp_status('form_isp_status');"">
<div class="row">
    <div class="tab">
        <?php $isp_tab=mysql_query("select distinct(isp) from red_dgid_info");
        while($result=mysql_fetch_array($isp_tab))
        {
            $isp_value =$result[0];
            echo "<input class='tablinks ion-radio-waves' type='submit' name='isp_value' value='$isp_value'>";
            // note: input is a empty tag, meaning that it is not to be closed using </input> but by using />, which
            // is only relevant for XHTML
            }?>
    </div>
</div> </form>

Edit: On the server side the only thing you have to do is use the value of $_POST['isp_value'].

var_dump($_POST); // only to check the POST variable during debugging

if (isset($_POST['isp_value'])) { // Possibly not needed if there are no other submit buttons in the from, but good practice to check if something exists
    // do something using $_POST['isp_value']
}

As a sidenote: mysql_* has been deprecated in PHP 5.5.0 and been removed in PHP 7.0. It is recommended to either use MySQLi or PDO instead

Jelmergu
  • 973
  • 1
  • 7
  • 19
  • k i removed the hidden field and then what i have to do to get the submitted tab value @Jelmergu – Puvi May 24 '17 at 06:56
  • Edited my answer. Note that the var_dump is only there to see what the post output is. If it is correct the value of isp_value should be the same value as the button you clicked on – Jelmergu May 24 '17 at 07:06
  • array(4) { ["fromdate"]=> string(0) "" ["todate"]=> string(0) "" ["pointer"]=> string(1) "1" ["isp_hidden_value"]=> array(9) { [0]=> string(7) "comcast" [1]=> string(5) "yahoo" [2]=> string(5) "gmail" [3]=> string(10) "roadrunner" [4]=> string(7) "verizon" [5]=> string(3) "att" [6]=> string(7) "hotmail" [7]=> string(3) "aol" [8]=> string(4) "juno" } } expect name='isp_value[]' other string values are displayed through var_dump($_POST) @Jelmergu – Puvi May 24 '17 at 07:17