Trying to echo JS function in PHP - php

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.

Related

Incorporating php variable into url with href attribute

I am trying to create the necessary url from this code however it is working and I am struggling to find out why.
$linkere = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linkere); ?>">'
Currently this code is producing the url: me.php?message= . But, I would like it to create the url: me.php?message=hello for example.
Thanks for helping!
You are passing $linkere to rawurlencode(). The variable is actually named $linker.
$linker = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linker); ?>">'
You have alot of syntax problems here.
first, you need to use Concatenation message='.rawurlencode($linker).'"
second your variable do not exist, it should be $linker.
Second close the tag and insert the text, in this case i used Test.
$linker = $row['message'];
echo 'Test';
Can you try this,
$linker = $row['message'];
echo 'YOUR LINK TEXT HERE';
You don't need the <? ?> and echo in your echo, it should just be:
$linkere = $row['message'];
echo 'Test';
Otherwise you are turning php on and off again to echo something within an already open instance of php in which you are already echoing.

Best way to combine PHP with HTML in CodeIgniter?

I've took over a PHP app which code is quite a mess so before making any major changes I've decided to rewrite it to MVC (CodeIgniter). As for pure html sections I use $this->load->view(file); technique, but I'm not sure how to cope with something like this:
echo "<tr>";
echo "<td class=\"border szczegolyTd\">";
echo "Kamp.: ".$kampania[$kamp]['idProject'];
echo "<br />";
echo "<b>".$kampania[$kamp]['name']."</b><br />";
echo "<div class='szczegolyDiv'><a class=\"pokaz szczegoly\" href=\"?pokaz=".$kampania[$kamp]['idProject']."\">";
echo "SZCZEGÓŁY";
echo "</a></div>";
if (isset($kampania[$kamp]['timestamp'][$iloLeftCustomers-1])) echo "<br />Dane od: <br /><b>".$kampania[$kamp]['timestamp'][$iloLeftCustomers-1]."</b>";
echo "<br />do: <br /><b>".$kampania[$kamp]['timestamp'][0]."</b>";
//echo "<br />".$ilOstatnichRozmow;
polacz();
$querySpr = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS ile FROM lista WHERE user=".$_SESSION['loginid']." AND kampania=".$kampania[$kamp]['idProject'].""));
rozlacz();
if ($querySpr['ile']==0) {
echo "<div id=\"".$kampania[$kamp]['idProject']."\" class=\"tabDodaj\">DODAJ DO OBSERWOWANYCH</div>";
}else{echo "<div class='komunikatMasz'>Masz tę kampanię na liście.</div>";}
echo "</td>";
I'm a beginner in CodeIgniter (and MVC in general), so I don't know all its features and which to choose. Perhaps it's not possible to seperate these 2 technologies completely - so it's more elegant to mainly use html and escape php scripts with <? ?> or to use php and echo html parts?
You can always write html in the template file and insert php in it to loop or echo.
echo "<tr>";
echo "<td class=\"border szczegolyTd\">";
echo "Kamp.: ".$kampania[$kamp]['idProject'];
echo "<br />";
Could be:
<tr>
<td class="border szczegolyTd">
Kamp.: <?php echo $kampania[$kamp]['idProject']; ?>
<br />
Putting code in those PHP tags wil actually fire the call in place. Mind you, to keep the html clean of bussines code make sure you only use echo, foreach, for or if in it.
In CodeIgniter you could render a view file and insert into a "partial" view and just echo it in the main template using TRUE for the 3rd parameter. This only buffers the output instead if immediatly outputting it.
$data['content'] = $this->load->view('ding/templatefile',$vars,TRUE);
if you are really new, please follow this tutorial its nice && informative.
yes it is possible, the best use is short tag + equal sign <strong><?=$variable?></strong> and it echoes whatever is in $variable, whenever you use foreach() use either
foreach($elements as $element) {
$string = "";
$string .= "new line" . $element->id . "<br>";
$string .= "line 2" . $element->name;
echo $string;
}
or just echo lines as you wish
foreach($elements as $element) {
echo "new line" . $element->id . "<br>";
echo "line 2" . $element->name;
}
but remember, before going in to foreach loop, please check if you have valid $variable to go thru. (empty(), isset(), $variable != FALSE...)
note:
if you need condition while echoing basic text use
<strong><?=(condition) ? 'TRUE' : 'FALSE' ?></strong>
example
<strong><?=(isset($variable)) ? $variable : '' ?></strong>
I would advise you go read up on the flow of how a MVC works, but this should point you in the right direction.
In the controller you instantiate the model of the data like:
$this->load->model("lista");
Then,
$data = $this->lista->getCountList($_SESSION['loginid'], $kampania[$kamp]['idProject']);
where in the lista model you have the function:
getCountLista($loginId, $projectId)
which does this part:
return mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS ile FROM lista WHERE user=".$loginId." AND kampania=".$projectId.""));
Then you pass the returned data to the view
$this->load->view(file, array("listData" => $data));
and in the view:
$listaData[$kamp]['idProject']
It creates an variable in the view foreach item in the array passed in the view.
I commend you to read documentation about MVC.

php simple concatenation problem

Here is a code sample of PHP function which prints HTML links. For some reason there is problem with the title attributte of the a tag(' games' isn't concatenated). For example if I have $gameCategorie = '3D' I get <a title='3D'>3D games</a> I want to get <a title='3D games'>3D games</a>
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title='.$gameContent.'>';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}
Ideas about improving the quality of the code and tutorials about HTML generation by PHP are also appreciated.
All valid xhtml should have attributes enclosed in speachmarks. Try this
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title="'.$gameContent.'">';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}

Having trouble with echoing HTML in php script

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.

calling function on hyperlink onclick event in php

can anybody help me here please?
I am using AJAX for pagination in my application. So I am generating hyperlinks with for loop. as follow:
for($t=1; $t<=$hf; $t++)
{
if($t == $_GET['pageno'])
{
echo $t." ";
}
else
{
echo "<a id ='$t' href='javascript:void(0)' onclick='open_page('ajaxinfo.php','content'); javascript:change('$t');'>$t</a>"." ";
}
}
Above echo statement does not give call to function. But instead of this when i just write html hyperlink it works fine and I get to see page2.html, my HTML code is:
<a id="page2" href="javascript:void(0)" onclick="open_page('ajaxinfo.php','content'); javascript:change('page2');">page2</a>
I don't understand why this is so? But is there any problem in echo's quotes.
Please Help.
that because you have syntax error while building anchors. Try to use double quotes for tag attributes and escape them with backslash.
So, your ECHO should look like this:
echo "<a id =\"{$t}\" href=\"javascript:void(0)\" onclick=\"open_page('ajaxinfo.php','content'); javascript:change('{$t}');\">{$t}</a> ";
You have to have code to add the contents returned by the ajax to the page. I don't see that anywhere.

Categories