Jump to content

pass 2 values from the suggestion list


aruksha

Recommended Posts

Could you please help me to pass 2 values from the suggestion list.

 

Related Data ;

PHP calls,

<script src="../js/mTools.js"></script>

<script src="../js/mSuggest.js"></script>

 

Codes,

<?php

include("*****.inc");

$resultABC = mysql_query("SELECT bud_id, bud_name FROM bud") or die(mysql_error());

$ABC=array();

$BgtId=array();

$counter=0;

while ($rowABC = mysql_fetch_array($resultABC))

{

$BgtId[$counter++] = $rowABC["bud_id"];

$ABC[$counter++] = $rowABC["bud_name"];

}

?>

//-------------------------

<FORM action='#' method=post>

<input name="BudName" type="text" size="50" rel="Suggest" autocomplete="off" data="<?php for($i=0;$i<count($BgtId);$i++) echo $ABC[$i].","; ?>" />

<input type='hidden' name='BgtID' value='<?php echo $BgtId; ?>'> 

                <input type='hidden' name='BdgtFrom' value='<?php echo $_POST['BdgtFrom']; ?>'>

<input name="Search_Voucher" type="submit" id="Search_Voucher" value="Search" class="Submit"/>

</FORM>

//------END------------

 

In here passes only "Bud_name".... I need to pass related value of Bud _Id too, when user submits form.....

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/127367-pass-2-values-from-the-suggestion-list/
Share on other sites

please include your POST handler script to help

 

one thing that caught my eye is that the input name is "BgtID" whereas the variable is "$BgtId"...

try doing this at the top of your post handler:

 

die(var_dump($_POST));

 

to see what variables and values its actually passing...

 

also, besides using more friendly variable names (helps ppl here figure out whats goin on!) here's a suggestion on how to improve part of your code:

 

MySQL Fetch

<?php
         include("*****.inc");   
         $result = mysql_query("SELECT bud_id, bud_name FROM bud") or die(mysql_error());
         while ($row = mysql_fetch_array($result))
         {
            $bud_id[] = $row["bud_id"];
            $bud_name[] = $row["bud_name"];            
         }
?>

 

no need to declare the arrays beforehand or use a counter! :)

 

I'm assuming that you're listing all of the "bud_names" in the textbox and whenever the user picks one it will the one of the hidden fields with the corresponding "bud_id" right?

 

since PHP is served as static HTML, there is no way of dynamically changing the value of "bud_id" using PHP, you'd actually have to use js instead:

 

HTML Form - AJAX Enabled

<FORM action='#' method=post>
<input name="bud_name" type="text" size="50" rel="Suggest" autocomplete="off" data="<?php for($i=0; $i<count($bud_name); $i++) echo $bud_name[$i]; ?>" onchange="updateHiddenField();"/>
<input type='hidden' name='bud_id' value=''>

<!-- rest remains the same -->

<input type='hidden' name='BdgtFrom' value='<?php echo $_POST['BdgtFrom']; ?>'>
<input name="Search_Voucher" type="submit" id="Search_Voucher" value="Search" class="Submit"/>
</FORM>

 

and then of course you'd have to create some JS to find the corresponding index of the selected "bud_name" and use document.getElementById('bud_id').innerHTML('foo') to fill it

 

but really, easier ways if you must use ajax would be to either:

1) retrieve only the "bud_name" and the "bud_name" input's onchange() or on the submit button's onclick() or even after POSTED query the DB to find the corresponding ID

2) store the selected "bud_name" array index somewhere (in this case you'd be better off going with the FOR instead of FOREACH loop) and then retrieving that value and populating the "bud_id" hidden field with it on the input's onchange() event

If i understood your problem properly, i wouldn't use AJAX at all, (lose the sexiness of suggest box, enter the practicality of select box)

 

i personally would have used mysqli or pdo here but keeping mysql for context

include("*****.inc");
$result = mysql_query("SELECT bud_id, bud_name FROM bud") or die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
    $bud[(int)$row["bud_id"]] = $row["bud_name"];
}

#### HTML ####

<FORM action='#' method=post>
<select name = "buds">
<?php
foreach($bud as $bud_id => $bud_name){
    echo '<option value="'.$bud_id.'">$bud_name</option>';
}
?>
</select>

<!-- last part remains unchanged -->

<input type='hidden' name='BdgtFrom' value='<?php echo $_POST['BdgtFrom']; ?>'>
<input name="Search_Voucher" type="submit" id="Search_Voucher" value="Search" class="Submit"/>
</FORM>

 

this way when you submit the form you will probably have both values (again do the var_dump) thing i mentioned in my first post and apologies if this doesn't make 100% sense its ridiculously late here...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.