Javascript variable with ajax-php variable - javascript

I have an ajax call to an external php file "commentS.php" that refreshes every 3 seconds. However. I expected to just use the variable in the hidden input field (name="idd") and expected it to just reflect on the "commentS.php", but, it does not. What I want to achieve is to have the variable "idd" echoed on the "commentS.php" page as a variable to be used in a mysql query. I did the document write on the commentS.php but it just shows a white page with the variable number that is "idd" when the ajax call is made.
<input type="hidden" name="idd" value="<?php echo $row['story_id']; ?>">
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
var named = "<?php echo $idd; ?>";
$(document).ready(function() {
setInterval(function () {
$('#content').load('commentS.php')
}, 3000);
});
</script>
Any help would be great, a read, anything.
I am very new to js and ajax altogether. thanks

Try this :
$('#content').load('commentS.php?idd=<?php echo $idd; ?>')
In commentS.php you have to do this in the beginning :
$idd = $_GET['idd']

Related

Change href depending on php response

I want to change the href of morestorieslink depending on the data received from mysql database i.e. if there is no row returned from server then the href for DOM element morestorieslink should change
I am using this for changing the href value
if (mysql_num_rows($result) == 0) {
?>
<script type="text/javascript">
document.getElementById("morestorieslink").href="select.php?<?php echo $selecturl?>=<?php echo $select; ?>&select=Continue";
</script>
<?php
echo "Url Changed";
}
In this case javascript doesn't work but the echo is displayed, I also tried embedding javascript inside php echo.
Thankyou
if (mysql_num_rows($result) == 0) {
?>
<script type="text/javascript">
// wait for dom to finish loading properly.
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("morestorieslink").href="select.php?<?php echo $selecturl?>=<?php echo $select; ?>&select=Continue";
});
</script>
<?php
echo "Url Changed";
}
Put this at the last of your page make it a point that no java script is executed after this. Rest all looks good.
Hope that helps.
Note :
the href you mentioned will append to the exsisting url.
Also check that $selecturl or $select is not null and isset.
You can also check the page source to check wether the link is generating properly or not (using Firebug in mozilla)
you can use ternary operator. Something like:
<?php echo $select ? $selecturl.$select : $otherURL;?>
I assumed your url is changing based on value of $select

how to dynamically change bootstrap alert test using php?

HTML:
<div id="signUpError"></div>
JS:
<script type="text/javascript">
function showSignUpError(message) {
document.getElementById('signUpError').innerHTML="<div class='alert alert-danger'>"+message+"</div>";
}
</script>
PHP:
$error="this is error"
How to call showSignUpError($error)
Try this code:
showSignUpError(<?php echo $error; ?>);
Try that first, but it might be necessary to quote the text:
showSignUpError("<?php echo $error; ?>");
Note that this will only run once, on page load.
If you wish to call-out to the PHP code while interacting with user, then you should check out AJAX:
AJAX request callback using jQuery

Passing a SESSION variable to JS

Okay, here's something that may seem rather trivial, but it's been giving me a headache, and I must have some sort of mental block now.
I want to pass a SESSION variable into JS, and then ultimately onto another page... It's only the first part that I'm worried about for now, because I can't even get a good "alert". The variable is blank... "the page at xxxxx says ____", even though the username shows up in the HTML span tag.
So, here's the applicable code on my page.
EDIT - For those asking why I would do this, I read it on (this) post...
EDIT: Solved. The method to do this is best done with:
var player = "<?php echo $_SESSION['username']; ?>";
Didn't need jQuery or other code. It is simple and direct, and shame on me for just sticking with one proposed solution from the other post.
In answer to some other questions: I did already have start session() at the top of the file. The script was after the HTML/PHP code. ***
... HTML stuff...
You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>
...more HTML...
<script>
function rollpublic()
{
... some declarations...
var player = document.getElementById("username").innerHTML;
alert (player);
more JS }
</script>
Edit
Sidenote: One reason which may be the cause of your session name not appearing, may be because session_start(); is not present in your pages, nor is there any mention of it in your posted code.
Without seeing FULL code, is hard to pinpoint it.
However, here is a successful test that I performed using the following codes: (page 1 & page 2)
Page 1:
<?php
session_start();
$_SESSION['username'] = "USERNAME";
?>
Check session name
Page 2: (sessions_check.php) which will echo and alert the session name.
<?php
session_start();
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var player = "<?php echo $_SESSION['username']; ?>";
alert (player);
});
</script>
You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>
Appearing in HTML source: var player = "USERNAME";
and You are <span id='username'>USERNAME</span></div>
N.B.: session_start(); needs to be inside all of the pages using sessions in order for this to work properly.
(Original answer) This worked for me:
<?php
session_start();
$_SESSION['username'] = "USERNAME";
?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(document).ready(function(){
var player = "<?php echo $_SESSION['username']; ?>";
alert (player);
});
</script>
You are <span id='username'><?PHP echo $_SESSION['username']?></span></div>
You may call session by ajax:
function getSession(){
var session;
$.get('someUrlThatReturnSession', function(sessionData) {
session = sessionData;
});
return session;
}
You can assign SESSION variable to javascrip variable..
<script>
function myFunction(){
var a = <?php echo $_SESSION['var'] ?>;
alert(a);
}
</script>
If you need more help let me know..
If you're using Angular JS, you can use this approach. (Provide the PHP var to the ng-init param of a hidden input)

How would I call a java script function using php if statement with $_SESSION

Hi I am creating a website with a login section this is working I am using HTML and PHP. What I am trying to do is one of my pages has a html button I want this to be disabled for certain users. at the moment this is what I have got.
this is the part that I use for the login details.
<?php
session_start();
$_SESSION["username"];
$_SESSION["password"];
$_SESSION["access"];
?>
I have got if statments that I am currently using which are
if($_SESSION["access"] == "Administrator"){
echo $Admin;
}
what I am trying to do is call a javascript function within a PHP if statement what i have got so far is
<?php
if($_SESSION["access"] == "Consumer")
{
echo '<script type="text/javascript">
Disable();
</script>';
}
if($_SESSION["access"] == "Administrator")
{
echo '<script type="text/javascript">
Enable();
</script>';
}
?>
the javascript functions that i am trying to call are
<script type="text/javascript">
function Enable() {
SubmitButton.disabled = false;
}
function Disable() {
SubmitButton.disabled = true;
}
</script>
I have also tryed
if($_SESSION["access"] == "Consumer")
{
echo "<script> Disable(); </script>";
}
Im just wondering if I have typed something in wrong or if I have forgotten to put something in.
any help would be much appreciated.
Looking at your code you have couple of issues:
Mixing your PHP logic and pure HTML is (usually) not a good idea.
Instead I would suggest you move your access checking logic fully on the server side and display the button accordingly (disabled or enabled) based on the user's access.
Example:
<?php if($_SESSION['access']): // Only show the button for users with access ?>
<button type="submit" value="Submit" <?php echo ($_SESSION['access'] != 'Administrator' ? 'disabled' : ''); // Button disabled for everyone but administrators ?> />
<?php endif; ?>
And let me point out the obvious (as mentioned by the other answers), that's not 100% bulletproof. The user can still manually submit the button even if he is not an administrator by editing the page's HTML on the fly. That's just a UI fix. The real check should be done on the server side once the button is submitted (e.g. is the user logged in, does he have a cookie on his computer that identifies him as an administrator, does he have a session cookie set, etc).
Calling JS in random places, e.g. in the header can have unexpected consequences.
You better wait for the page to be loaded fully before calling any JS functions. You can do that via jQuery easily, but make sure you include the jQuery library before that in your header like so.
Afterwards you can call any JS after the page is loaded by placing them within the following block:
$(function(){
// Place your JS calls here, e.g. call to Enable()
});
String concatenation in PHP is done with a dot . and strings can be multiline
This code which you used is just plain wrong.
echo '<script type="text/javascript">'
, 'Enable();'
, '</script>';
You should use something like:
echo '<script type="text/javascript">'
.'Enable();'
. '</script>';
or better:
echo '<script type="text/javascript">
Enable();
</script>';
PHP doesn't use , sign for joining. Use ..
But otherwise it should work, except that you should define SubmitButton in advance of using it.
<?php
echo "<script type='text/javascript'>";
// if the id of your element is "submitButton"
echo "var submitButton = document.getElementById('submitButton');";
echo " function disable(){ submitButton.disabled=true; }";
echo "</script>";
?>
After that you can use it as you did..
<script type='text/javascript'>
disable();
</script>
Just be advised that denying access to some elements/functionality on your webpage with JavaScript alone is not a good practice - JavaScript is executed locally on the user's computer and therefore the user can modify it to gain an advantage.
Well, the problem may be that you're trying to call the javascript function before the HTML is ready (or finally rendered), so the browser, when executes the function doesn't find the button.
You could solve this placing your javascript code at the end of your page, or using jQuery and doing:
$(document).ready(function() {
<%php if ($_SESSION['access'] == 'xxxxx') {%>
Enable();
<%php } else { %>
Disable();
<%php } %>
});
Anyway, ALWAYS check user permissions on the server side, because someone could enable the button using Firebug or something else...

How to use form value without submitting the form to same page or to other page in php

// I want to use a form value with out submitting to a page or within the same page
//Javscript
<script language='javascript'>
function L()
{
//Here am setting the value for the hidden filed
document.getElementById('ide').value='VALUE';
}
</script>
// Here i want to echo out the value of hidden field
<body onload=L()>
<?PHP
echo "<input type='hidden' id='ide'>";
//Here i want to echo out the value of hidden field
//How can i do dat using PHP
?>
</body>
enter code here// Here i want to echo out the value of hidden field
// I want to use a form value with out submitting to a page or within the same page
// I want to use a form value with out submitting to a page or within the same page
First thing, if it is a hidden field, how do you expect it to show the value.
Your code is correct, just change hidden to text as below,
<script type="text/javascript" language='javascript'>
function setValue()
{
document.getElementById('ide').value = 'your value is 10';
}
</script>
<body onload=setValue()>
<?php
echo "<input type='text' value='' id='ide'>";
?>
</body>
Better use a div or span as below,
<script type="text/javascript" language='javascript'>
function setValue()
{
document.getElementById('ide').innerHTML = 'your value is 10';
}
</script>
<body onload=setValue()>
<?php
echo "<span id='ide'></span>";
?>
</body>
you do realize that PHP is a server side scripting language right? The PHP code you write is parsed into HTML in the web server, So you must submit it to a web page, If you want to do it without submitting to a webpage you should use java script ( jQuery ).

Categories

Resources