jQuery prepend() function alternative in php [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to switch a task of jQuery to PHP.
Earlier it was like :
myString += "Hello There ! ";
$("#latestRoutes").prepend(myString).slideDown(2000);
But now As I am switching to PHP, How can I do the $.prepend function in PHP to append a variable to a particular string ?

On page load if the variable isset then show it as a first child of your div like this:
<div>
<?php if(isset($mystring)) echo '<p class="test">'.$mystring.'</p>'; ?>
<p>...</p>
<p>...</p>
</div>
but since this is php is served only on page refresh and of course you cannot have fancy js animations at once, some extra work needs to be done. If you want this to communicate with js add a class there and with js check its existence and then start its animations. This can be done like:
if($('.test').length > 0) {
alert("This message is brought by php, great!!!");
$('.test').on('click', function(){
$(this).hide();
});
}

Related

How to call a function from button inside echo in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to pass a variable from one php function to another.
1st function creates a button.
2nd function needs to be able to receive variable that button sent to the 2nd function.
Nothing happens when i click the button. Page just flickers.
<?php
function myFunc(){
$filename = "123";
echo '<form method="post"><input type="submit" name="button1"class="button" value="Button1" onclick="myOtherFunc("tomato")" /></form>';
}
myFunc();
function myOtherFunc($filename){
echo '$filename'."anyting ?" ;
}
Onclick will not execute php code. PHP code is executed on the server BEFORE your page is rendered. Once your page is rendered, the only code that can be executed without reloading the page is Javascript. The onclick attribute on your button will look for a Javascript function called func_ImageView, which does not exist. If you want the content if your func_ImageView function to be called, you need to call that code in php, but you should instead use javascript for this from what it looks like.

How to auto save html textarea without button? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a textarea in my html like this:
<textarea id="area1"></textarea>
When visitors copy/paste or type something in this area i want it to save to a txt file without the visitor having to click any button.
Looked all over the web but can't find any solution.
write JS function, that saves info in .txt file. Let's say, function name is saveToTxt(). Then trigger that function onChange:
<textarea id="area1" onChange="saveToTxt(this);"></textarea>
EDITED
Assume that, saveToTxt() is something like that:
<script>
function saveToTxt(fld) {
const textAreaValue = fld.value;
// then use textArea variable as container of textarea-content
// and then treat it as you want.
}
</script>
This example show how to save content automatically 2s after changing. It can prevent from doing save for every character typed.
var t;
function save() {
clearTimeout(t);
t = setTimeout(function() {
console.log('All changes saved'); // save here
}, 2000);
}
<textarea onchange="save();" onkeyup="save();"></textarea>

How can I specifically convert this php code to html? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
how can I specifically convert this php below code to html? I want to do this as I want to be able to run this code on my website.
From reading other forum posts I understand about people saying them link the PHP file externally using something like this php. However, from my understanding my PHP code is slightly different in terms of the output and I'm just not sure.
Below is my php code I want to run externally on my site from the html file.
<?php
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>
Can you use Ajax and show the result in HTML
<script>
$.ajax({
urL:"./tomy.php",
type:"POST",
data:{getuserAgent:1},
success:function(data){
$("#mydiv").html(data);
}
});
</script>
tomy.php
<?php
if(isset($_POST['getuserAgent']){
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
}
?>

Hide div before login [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a table with add, edit, delete options. Now I want to hide divs with buttons for not logging users.
Or maybe I should make a deactivated buttons for not loggin users?
when user login then you have session like
$_SESSION['email']=$email;
then used this code
<?php
if(isset($_SESSION['email']))
{
echo "<div>any text</div>";
}
?>
do some effort to write the code
Depending on how your site works there are two possible solutions that I can think of.
USE PHP
If the user is logged in then use:
<?php
if ($logged_in) {
?>
<div>Your text here</div>
<?php
}
?>
NOTE: You do not need to use "echo" or "print" if you close and open your PHP tags between the "if" as shown below. This can make the HTML part of the much easier to read and write.
USE JAVASCRIPT
If the page does not reload when the user logs in then you will need to use Javascript to hide and show the DIV using something like:
var x = document.getElementById("myDIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}

Using php condition and javascript hide the div's [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I just messed up with is a problem from the last couple of hours and still didn't get the solution i m new in php.what I m trying to do is display and hide the dive according to php condition.
here is the scenario:
note: by default DIV(display:none)
if(condition is satisfied ) //condition in PHP only
//call the javascript function which displays the div
else
//call the javascript function which hides the div
if you have the solution with example please must share it's a humble request
thank you
You want to mix Javascript, HTML and CSS as little as possible. It might look easier to mix it right now, but that will change as you get more experience.
There are multiple ways, but one I prefer is running PHP and passing the variables to a JS variable and let JS decide:
const showSpecificElement = <?php echo funtionWhichDoesSomething(); ?>;
if( showSpecificElement ){
// Javascript magic here
}
// Or, alternatively:
if(<?php echo funtionWhichDoesSomething(); ?>){
// Javascript magic here
}
// Or, slightly better:
$classWhichHidesThisDivOrNot = rand(1,2)==1 ? 'hideMe' : 'showMe';
<div class="<?=$classWhichHidesThisDivOrNot?>"> ... </div>

Categories