I am trying to make a form that show users name and I am having a little trouble with this part. The ' ' and " " marks dont quite work how they are supposed to. Im trying to echo the options in drop down menu and some how the $wholenames and the last " sign appear in the wrong part of the page. Could someone please tell me what is the correct way of doing this?
Thanks
echo' "<option>'; echo $wholenames; echo'</option>"';
Actually I had looked it wrong it is a little bit more complex. Below you can see the code. The whole dropdown menu does not appear. The wholenames integer appears, but the menu does not...
echo'
<label for="addusertogroup">Add user to an existing group:</label>
<select name="addusertogroup" id="addusertogroup">
'; if(mysql_num_rows($userresult))
{
while($row2 = mysql_fetch_assoc($userresult))
{
$wholename = array("$row2[f_name] $row2[s_name]");
foreach ($wholename as $wholenames) {
echo "<option>$wholenames</option>";
}
}
}
else {
echo "<option>No Names Present</option>";
}
To make it work, simply do this:
echo "<option>";
echo $wholenames;
echo "<option>";
or this:
echo "<option>$wholenames</option>";
or this:
echo '<option>'.$wholenames.'</option>';
All will work, just up to you which one you pick.
You shouldn't have " before <option> and after </option>
It should be something like
echo "<option>". echo $wholenames; echo "</option>";
Also, if $wholenames is an array, you'd better iterate over it:
foreach ($wholenames as $name){
echo "<option>". echo $name; echo "</option>";
}
Any text for options in a HTML SELECT box are written inside the tag. If you don't put your text between the <option> tags the browser will try to insert it to the select box's DOM.
So you could change your code to this:
echo '<option value="myvalue">"' . $wholenames . '"</option>';
Haven't actually tested this code.
Update if the Quotation mark was not meant to be in the output you would simply need to write:
echo '<option value="myvalue">' . $wholenames . '</option>';
In php you can use both " " and ' ' with strings.
Related
I'm trying to be able to get place as a variable to use on page.php. It will start to load options into the select box for about 5 seconds and I'll actually be able to see the options, then it all deletes and I have an empty select box. Here's the PHP code:
<?php
$xml = simplexml_load_file("file.xml");
echo '<form method="get" action="page.php"><select name="place" id="place">';
foreach($xml as $banana)
{
if(isset($banana->id))
{
$id = $banana->id;
$string = $banana->state . " - " . $banana->name;
echo '<option value="' . $id . '">' . $string . '</option>';
}
}
echo "</select>";
echo '<input type="submit" /></form>';
?>
I know for sure that my XML is formatted correctly, but the XML file has a little over 2500 entries that each looks something like this:
<container>
<id>theId</id>
<state>theState</state>
<name>theName</name>
</container>
Any ideas why won't load?
Note: Some items have an empty id in the XML file, and I don't want to include those files, that's why I check to make sure it's set.
A couple things
Disable any javascript and try again--something may be binding to overriding the select box or the id of an element in it.
You generally want to properly escape text you pull in before inserting it into a HTML document in case there's a <, >, or such in it
Try the following:
echo '<option value="' . htmlspecialchars($id) . '">' . htmlspecialchars($string) . '</option>';
Please help me to understand syntax of concat following
i want following code inside echo to understand concate
1--> <div class="alert echo $_REQUEST['error_id'];?>"\>
I try following code but getting syntax error that there is two echo..
2--> echo "<div class=\"alert" . echo $_REQUEST['error_id']; . "\">";
where mistake is that i cant get it... and i want two answer both using single quote and double quote
EDIT
Thank #Rizier123 now it working but css is not working as i have apply in calss="alert"
while using following code its working fine
<div class="alert echo $_REQUEST['error_id'];?>"\>
But after applying your code its not working..only text appear but background color is not comming nad boarder is also dissappear as below
class name is alert shows the status of login..
EDIT
Thanks again i just forget to put space after class name..
You don't need to write echo and ; again!
So this should work:
//echo with double quotes
echo "<div class=\"alert" . $_REQUEST['error_id'] . "\">";
//echo with single quotes
echo '<div class="alert' . $_REQUEST['error_id'] . '">';
Maybe you need to add a space if your class name includes spaces!(..."alert "...)
try this
echo '<div class=\"alert"' . $_REQUEST['error_id'] . '">';
You can do either 2 ways
1. PHP in HTML
<div class="alert<?php echo $_REQUEST['error_id'];?>">
2. HTML in PHP
echo "<div class='alert". $_REQUEST['error_id'] ."'>";
Or you can also do like
echo "<div class=\"alert". $_REQUEST['error_id'] ."\">";
I am attempting to create a drop down of items from a JSON file located on a remote server. The drop down appears to be populating (as there are options to choose from), but the text is not visible. I have attempted changing the style color (worth a try, right?) and multiple browsers.
<?php
echo '<select name="version" style="width: 300px">';
$url = 'http://s3.amazonaws.com/Minecraft.Download/versions/versions.json';
$jsonData = file_get_contents($url);
$jsonDataObject = json_decode($jsonData);
foreach($jsonDataObject->versions as $option){
echo '<option value=' . $option->type . '</option>';
}
echo '</select>';
?>
Thanks in advance for any assistance offered.
You are not populating the display text
echo "<option value= { $option->type } >{$option->type}</option>";
This is a little different from your statement, but the logic is the same. You need to write some text between option tags
<option value="val">displayText</option>
It should be something like-
echo '<option value=' . $option->type . '>'.$SOME_VALUE_HERE.'</option>';
// ^
I'm trying to echo a js function in HTML string inside PHP echo.
And I can't figure it out :
$MY_JS_FUNCTION = '<script type="text/javascript">item + getLastField2()</script>';
if($row2->type == 'text') {
echo "<li id='item-".$row2->rank."' class='list_item'>";
echo "<textarea rows='2' id='".$row2->single_id."' cols='90' name='field[".$MY_JS_FUNCTION."]' data-kind='text' >".$row2->content."</textarea>";
echo "</li>";
echo '<br />';
}
Any ideas to get this work? I think I have so much quotes in it or something like that...
Any help would be very very appreciated, thanks!
I'd reccommend storing the name in the database as well.
Then you can use $row2->name to insert the right name
Your variable $MY_JS_FUNCTION contains an HTML <script> tag with some (strange) JavaScript code (missing a semi-colon). Based on your code the echo on line 5 results in this HTML:
<textarea ... name='field[<script type="text/javascript">item + getLastField2()</script>]' ... >...</textarea>
This is definitively not valid HTML. And there is your problem...
It appears your intent is to echo that JS so that when the page loads, the JS actually sets the value of the name field for that textarea. If that's the case, a simpler way might be something like this:
$MY_JS_FUNCTION = '<script type="text/javascript">document.getElementById("myTextArea").name = item + getLastField2()</script>';
if($row2->type == 'text') {
echo "<li id='item-".$row2->rank."' class='list_item'>";
echo "<textarea rows='2' id='".$row2->single_id."' cols='90' id='myTextArea' data-kind='text' >".$row2->content."</textarea>";
echo $MY_JS_FUNCTION;
echo "</li>";
echo '<br />';
}
That will produce valid HTML. The JS function will fire once that line is reached and update the "name" value to whatever the result of the function is. Be sure to add the "id" field so that the JS knows which element to target.
So... I have a mysql_fetch_array and I'm running into an issue when some of the mysql data contains single or double quotes. This is the dumbed down version of my code:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
The edit_form() function is used to send the value of the current item back to the value of the input in the form so the user can then easily edit their entry and then sends an UPDATE command to mysql along with the proper primary key id (which I left out because of irrelevancy). The only issue I have is if a user puts single or double quotes into the form then it messes up the onclick attribute. Please help!! I am pretty new to php and can't figure this out. I've messed around with htmlentites() and html_entity_decode() but am still getting no where. Thank you so much!
Use htmlspecialchars on $row['item'] before inserting it in your document.
So your "dumbed-down" code should be:
while($row=mysql_fetch_array($list)) {
$item = htmlspecialchars($row['item']);
echo "<tr>";
echo "<td onclick='edit_form(\"" . $item . "\");'>" . $item;
echo "</td></tr>";
}
Try the below line instead of the original in your code and see if it works:
echo "<td onclick='edit_form(\"" . str_replace('"',""e;",$row['item']) . "\");\">" . $row['item'];
And where you would like to display the $item field, just replace it in reverse if you get "e; instead of ' " '. For example:
$qoute_free= str_replace('"e','"',$passed_value);
If you are using it in javascript, the function can be as below:
function edit_form(passed_value)
{
new_value=passed_value.replace(/"e;/g,'"');
}
I advise using json_encode. That way you don't have to worry about special cases htmlspecialchars might miss (such as newlines).
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(" . json_encode($row['item'], JSON_HEX_APOS) . ");'>" . $row['item'];
echo "</td></tr>";
}
I'm just putting the values back into my <form> (when they click on a <td>) and when they hit SUBMIT it directs the data to a different .php file that uses a MySQL UPDATE instead of an INSERT. I finally found some code that works!
$list is a mysql_query I ran at the top of the document
$item is the title of one of my columns in MySQL
while($row=mysql_fetch_array($list)){
$item = json_encode($row['item']);
$item = str_replace("'","'",$item);
echo "<td onclick='edit_form(" . $item . ");'>" . $row['item'] . "</td>";
}
Now it display's correctly in the <form> when <td> is clicked (by jQuery input.val($item)) and it shows up in the table correctly via $row['item']. I don't really understand exactly how it's working (how the encode is making it work) but I am glad it is. Crazyness!!!! Thanks for responding! and thanks for your effort!!