how to get text instead of value-php and wordpress - php

I need help in getting the text instead of value.
I have gone through all the websites some of them says use javascript into this.
This is the plugin.(si contact form)
Here if i do $_POST it gives [array([0]=>1)] only value not text.
Below is the code i have searched where they are framing.
$string .= '<option value="' . $opts_cnt.' "' . $selected . '>'.esc_attr($opt).'</option>'."\n";
$opts_cnt ----->value
$esc_attr($opt)-------> text
i tried changing this way
$string .= ' <option value="' . $opts_cnt.' "." '.esc_attr($opt).' "' . $selected . '>'.esc_attr($opt).'</option>'."\n";
i tried in many permutation and combination ways.
"." if i am not inserting this its gives error message
At last i need the code to be in this format
$string .= ' <option value="' . $opts_cnt.' '.esc_attr($opt).' "' . $selected . '>'.esc_attr($opt).'</option>'."\n";
Even this is not working.

If you remove the value attribute from the option tag then you should get the actual text for the option:
Try:
$string .= '<option ' . $selected . '>'.esc_attr($opt).'</option>'."\n";

Related

How to populate select options from DB

What am I doing wrong here? When it populates the options it shows $name[0] and not the info from the DB. Though the correct number of options seem to be available.
<?php
//connect to database
$conn = mysqli_connect("example.com", "timemin", "Pass123", "timesheet");
//query database for items to populate
$sql = "SELECT DIS_NAME, NAME FROM INVITEM";
$query = mysqli_query($conn, $sql);
echo '<select>';
echo '<option value="">Choose your favorite fruit</option>';
while($name = mysqli_fetch_assoc($query)){
echo '<option value="' . '$name[1]' . '">' . '$name[0]' . '</option>';
}
echo '</select>';
echo $query;
?>
Should be as follows, you quoted variables, that was the problem.
echo '<option value="' . $name['DIS_NAME'] . '">' . $name['NAME'] . '</option>';
Also as RiggsFolly mentioned, you used fetch_assoc so the array keys will be named accordingly DIS_NAME and NAME.
Credit to RiggsFolly for spotting this.
In php, there are two types of quotes: single quotes and double quotes. Single quotes will not parse variables, double quotes will.
If you do want to use quotes, you could do something like this:
echo "<option value="."$name[1]".">$name[0]</option>";
So here, the double quotes will tell php to parse the variable names
However, I would recommend doing this:
echo '<option value="' . $name[1] . '">' . $name[0] . '</option>';
See this SO post for more.

Advanced Custom Fields – Custom Field Type with multiple inputs

I am trying to create a new field type for ACF that contains multiple inputs or stores an array of values. The reason is that I would like to have some interactivity and a custom layout for a group of input fields.
I followed this tutorial http://www.advancedcustomfields.com/resources/tutorials/creating-a-new-field-type/ and used the provided template: https://github.com/elliotcondon/acf-field-type-template which is really nice and well documented. Storing one value is pretty simple. I am using only this function from the template:
function create_field( $field )
{
echo '<textarea id="' . $field['id'] . '" rows="4" class="' . $field['class'] . '" name="' . $field['name'] . '" >' . $field['value'] . '</textarea>';
}
What do I have to change in order to use two or more inputs?
Thanks!
The names and values of your 2 textareas must be as follows:
echo '<textarea id="' . $field['id'] . '" rows="4" class="' . $field['class'] . '" name="' . $field['name'] . '[textarea1]" >' . $field['value']['textarea1'] . '</textarea>';
echo '<textarea id="' . $field['id'] . '" rows="4" class="' . $field['class'] . '" name="' . $field['name'] . '[textarea2]" >' . $field['value']['textarea2'] . '</textarea>';
textarea1 and textarea2 can be chosen freely by you.
This will save multiple values in your custom field type, value will save an array like:
Array
(
[textarea1] => abc
[textarea2] => xyz
)
Did you check the flexible content add-on for ACF?
I think you will find the solution in that code.
Oh, and if you find the solution please post it here, because i'm really interested in the solution. Sorry to get your hopes up by posting a not so clear answer.

Dynamically setting the selected option in a HTML drop down list - PHP

I am aware there are examples out there that hover around the issue of dynamically setting the selected tag in a HTML option tag, but I am hitting a pretty difficult issue of break statements and quotations in what I am trying to accomplish.
while($info = mysql_fetch_array($companydesc))
{
$output3 .= '<option value="'. $info['company_code'] . if ($result['company']==$info['description']){echo 'selected=\"selected\"'} . '">' . $info['description'] . '</option>';
}
echo $output3;
The error that I receive is a unexpected T_IF on the line with the if statement. Is it not legal to put an if statement in there? Or is it a matter of doing proper breaks? Any help would be greatly appreciated (and hopefully the formatting for the code worked)
Use a ternary statement:
while($info = mysql_fetch_array($companydesc)) {
$output3 .= '<option value="'. $info['company_code'].'"'.($result['company']==$info['description'] ? ' selected=\"selected\"' : '') . '>' . $info['description'] . '</option>';
}
echo $output3;
yeah, it is illegal, my suggestion is:
while($info = mysql_fetch_array($companydesc))
{
if($result['company']==$info['description'])
{
$output3 .= '<option value="' . $info['company_code'] . '" selected = selected>' . $info['description'] . '</option>';
}
else
{
$output3 .= '<option value="' . $info['company_code'] . '">' . $info['description'] . '</option>';
}
}
echo $output3;
Trying to fix everything with an individual solution in the way that you originally intended to with stuff like
while($info = mysql_fetch_array($companydesc))
is, I feel, one of the big things that is holding php back and eventually makes the code extremely difficult to maintain, although it's very easy on the learning curve.
This function takes an optional selected parameter and will build out your select box. This is much much better than doing every single select box for every query with its own custom code.
There might be a syntax error in here, but generally,
function selectBox($array, $selected=false){
foreach($array as $name => $value){
$options .= '
<option value=" . $name . '"'
. ($selected != false && $selected == $value) ? 'SELECTED' : ''
. '>".$name."</option>";
}
}
Worth mentioning that select box is requiring you to pass a predefined array of key=>value pairs, so you'll need to have an additional helper function (you can't just pass it your database return)
Also probably worth mentioning is that using mysql_fetch_array($companydesc) is not abstracting the database level at all. Down the line, code like this will prohibit a migration to something like Postgres or a different database system entirely.

options are not getting printed/echoed?

This page generates the option pulled from database . another page brings here the year of joining of students via year_joining . rest of the mysql queries works absolutely fine ( tested )
<?php
include_once("../Include/connectdb.php");
if($_GET['year_join'])
{
$id=$_GET['year_join'];
$result1 = mysql_query("select distinct sub_id from subject_profile where batch='$id'
")or die(mysql_error());
while($subid = mysql_fetch_assoc($result1)){
$result2 = mysql_query("SELECT name FROM `subjects` WHERE `sub_id` LIKE
'$subid[sub_id]'");
$subject=mysql_fetch_assoc($result2);
if($subject[name]!=""){
//print "<OPTION value=".$tmp.'">'.$tmp.'</OPTION>';
//print "<OPTION value='$tmp'>'$tmp'</OPTION>";
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
//echo '<option value="'.$id.'">'.$data.'</option>';
//}
}
}
}
?>
FYI :
//print "<OPTION value=".$tmp.'">'.$tmp.'</OPTION>';
//print "<OPTION value='$tmp'>'$tmp'</OPTION>";
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
//echo '<option value="'.$id.'">'.$data.'</option>';
none of these are working ... :(
with simple echo $tmp it works
but when ever i put as
echo "<option value=";
the result is blank page ...
and when i am echo - ing just the variable its works perfectly fine
echo $tmp;
gives the list of all the subjects ..
For the problem regarding "not displaying output", I believe you have missed a <select> tag enclosing the <option> tags.
As a side note, in the query, change it like this:
"SELECT name FROM `subjects` WHERE `sub_id` LIKE '{$subid[sub_id]}'"
or,
"SELECT name FROM `subjects` WHERE `sub_id` LIKE '" . $subid[sub_id] . "'"
Still it is not safe. You should escape the values and better use a prepared statement using mysqli or PDO
The main problem is as per Akhilesh B Chandran's answer, with a missing <select> tag.
Other than that, there will be a problem when $subject['name'] has a space in it.
The problem is this line:
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
It should be like this:
echo '<option value="'.$subject['name'] . '">' . $subject['name'] . '</option>';
In what you currently have, the browser interprets the result as this:
<option value=insert name here">insert name here</option>
As it is visible, there is a missing quote after value=.
Your quotes don't match up, which is why PHP is failing.
Try changing:
echo "<option value=".$subject['name'] . '">' . $subject['name'] . '</option>';
to:
echo "<option value=".$subject['name'] . ">" . $subject['name'] . "</option>";
Basically, you start off using a ", and switch partway through to using '

How to Populate Dropdown List's text and value from MySQL?

I am using mysqli to query a database table to obtain the value for a dropdownlist. However, I also want to retrieve the corresponding description and load that into the option text. My query is as follows:
<?php
$query=('SELECT restaurantid,location from restaurant');
$result = mysqli_query($mysqli,$query);
echo '<select name="ddlStore">';
while($row=$mysqli->fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">';
'</option>';
}
echo '</select>';
?>
How do I revise the above so that the location field is populating in the dropdownlist's option text?
Updated code:
<?php
$query=('SELECT restaurantid,location from restaurant');
$result = mysqli_query($mysqli,$query);
echo '<select name="ddlStore">';
while($row=$mysqli->fetch_array($result))
{
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' .
htmlspecialchars($row['location']) .
'</option>';
}
echo '</select>';
?>
The above still does not display the locations as dropdownlist option text values.
Update #2:
When viewing the page, the dropdownlist doesn't show the values from the database table. Using IE Developer Tools, I see a script error in the while statement:
Call to undefined method mysqli::fetch_array()
Is there a more optimal way to structure the mysqli_fetch_array statement?
Change to...
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' . htmlspecialchars($row['location']) . '</option>';
echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' . htmlspecialchars($row['location']) . '</option>';
But what have you tried so far?
Try this way (more info here)
$result = $mysqli->query($query)
while($row = $result->fetch_assoc()) {
}

Categories