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.
Related
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 3 years ago.
Improve this question
i want to redirect index.php which if user click on logout button it redirects to login.php page but it don't.
http://localhost:/dropedit/GAMEBOX
and if some one type like following it went all wrong like,whole page becomes garbled
http://localhost:/dropedit/GAMEBOX/index.php
logout button code is Logout ?
i tried but din't find some thing!
Make the login button a part of a form, and have the form submit to index.php. The form will contain an empty input that tells the page that you have been logged out. For example:
<form action = '/' method = 'post'>
<input style = 'display: none;' name = 'logout' value = 'logoutTrue">
<input type = 'submit' value = 'Logout'>
</form>
Next, a simple if statement to check if you have been logged out:
<?php
if (isset($_POST['logout'])) {
$logoutVal = $_POST['logout'];
if ($logoutVal === "logoutTrue") {
header("Location: http://localhost/login.php");
}
}
?>
Uh, this should work, I think? If it doesn't please post the error codes.
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>
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 8 years ago.
Improve this question
I have a php page wich has a button (index.php), and this php page contains variables and I want to use them in another php script(script.php) in another file but at the same time, I want the user to be redirected to an html page (success.html)
How do I do that?
Set your <form action="script.php" method="post"> in index.php and put this line of code at the end of your script in script.php:
header('location: 'success.html']);
How are you passing your variables to script.php?
If you put this script in script.php:
foreach ($_POST as $key => $value){
$$key=$value;
}
Then it will create variables for all of your form elements with a name="" atribute, and assign them their respective values.
Example:
<input type="text" name="firstname" value="John">
will create the variable
$firstname = 'John';
This is very simple there are several ways of doing this, you can use sessions or set a form to posts the results. So on index.php you could do something like this
<?php
session_start()
$_SESSION['foo'] = 'bar';
header("location:success.html");
?>
you now have global access to $_SESSION['foo'];
<?php
echo $_SESSION['foo']; // will output bar
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();
});
}
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 am trying to pass JS variable to php echo statement.
here is my code
<?php print 'test'; ?>
how can I do that
I just test this and it works but how can I write the value within the href
alert(myvalue); ";
This is impossible from a temporal point of view.
When the PHP code is run, there is no JavaScript. The page is not yet loaded into the browser. There is no JavaScript engine being run. There are no JavaScript variables. By the time the browser renders the page, the server code has already done its job.
With that said, you can render HTML which refers to a JavaScript function. For example:
<?php print 'test'; ?>
Then, implement DoNav accordingly:
function DoNav(url)
{
location.href = url + '?id=' + my_JS_value; // Navigate to the new URL with the JavaScript variable
}
JavaScript and PHP cannot directly communicate as JavaScript is client-side and PHP is server-side, i.e. PHP is executed before the JavaScript is sent to the browser.
The only way to achieve what you want is to sent a request from JavaScript that calls a PHP script (e.g. AJAX) and passes the variable via GET (like your example) or POST.
Javascript code is executed on the client side, so you do not have access to it.
You can just create the link using Javascript, like this:
var my_JS_value = 0;
document.write('test');
You can insert the element with php and set the href attribute later with JS. Anyway there's a ton of other ways to achieve the same.
<?php
print '<a id="mylinkelement" >test</a>
<script>
var mylinkelement=getElementById("mylinkelement");
mylinkelement.href="thisTest.html?id="+my_JS_value;
</script>';
?>
You don't even need php for that :D