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

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);
}
?>

Related

PHP echo at an earlier line of the script [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 7 months ago.
Improve this question
I have a PHP script that generates html. In the bottom part of the script I do some computations using PHP. I want to echo the result in the top part. Is this possible.
//script begins
<?php
....
//I want to echo the result here
....
....
....
//PHP computations, result in $result
//I do now want to echo result here but in the top part of the output document.
?>
//script ends
More Information- The output is an html document. I do some computations in the body part for which there is no workaround. I want to echo the output in the head part.
You need to make the computation where you need it. like this
<?php
// computations here and store the values in variables
$value = 'xyz';
// or define a function here that you can use
function func ()
{ return false; }
//html here, some exmaples
?>
<div><?php echo $value;?></div>
<h1><?php echo func();?></h1>
?>

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>

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>

jQuery prepend() function alternative 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 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();
});
}

Categories