After looking around on a Google without any success, i feel posting here may be a good idea as I have used this site to answer previous questions.
Anyways, I am currently working on an HTML5 canvas game using; PHP, MYSQL, Html5, and JavaScript.
I have MYSQL databases setup and an PHP page displaying player high-scores, and usernames.
My question is how would I go about displaying the high-scores inside the canvas once the game is over.
As well as saving the high score when the game ends. I've looked on W3SCHOOLS site about AJAX but I'm still unsure of what codes to use inside the JavaScript file.
These are my php/script codes. or at-least the ones that are relevant:
// Here's the savescore.php file
<?php
include 'connect.php';
$user_score = ($_POST['user_score']);
$user_name = ($_POST['user_name']);
if(mysql_query("INSERT INTO users VALUES('$user_name','$user_score')"))
echo "Score Successfully Saved";
else
echo "Score Saving Failed";
?>
// Here's some of the index.php file
<link rel="stylesheet" href="css.css">
</HEAD>
<body>
<div id="menu">
<a class="item" href="/index.php">Home</a>
<?php
include 'connect.php';
session_start();
if($_SESSION['signed_in'])
{
echo 'Hello ' . $_SESSION['user_name'] . '. Not you? Sign out';
include 'chat.php';
}
else
{
echo 'Sign in or create an account.';
}
?>
</div>
<BODY>
<canvas id="canvasGAMEOVER" width="800" height="599"> </canvas>
<script src="game.js"> </script>
// here's whats inside inside game.js... well the part I want to be able to save score
var score = 0;
function drawGAMEOVER() {
}
I have used google and looked at tutorials for AJAX, I found I have been able to connect to the server using AJAX only using:
<form action="savescore.php">
user_name: <input type="text" name="user_name"><br>
user_score: <input type="text" name="user_score"><br>
<input type="submit" value="Submit">
</form>
inside the index.php page, but I am not sure if its possible to grab the 'user_name' they logged in with (displayed on the index.php page) as well as this.score (displayed inside the javascript file.)
Could anyone tell me how this is possible... if not maybe a better way of doing this?
Any help/reply is much appreciated thanks in advance.
If you are using jquery you can use $.get to get all highscores.
http://api.jquery.com/jQuery.get/
Related
I am trying to make a form on my website where users can submit their name and their score will get saved to a list of highscores. (its a quiz game.)
I tried learning to use forms using w3schools. I used this example: https://www.w3schools.com/php/php_forms.asp
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
with welcome.php looking like this:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
I literally copy and pasted this example and tried running it and i get this error message:
WelcomeWarning: Undefined array key "name" in [filelocation] on line 4
Your email address is:Warning: Undefined array key "email" in [filelocation] on line 5
However when I replaced all the "post" with "get" it worked. Why? What do I need to do to get it to work with post?
EDIT: Also I left the "post" in the html but i replaced the POST in the welcome.php with REQUEST. It now works, however I think its somehow using GET instead of POSTs because i can see the input in the URL. I definitely need to avoid this.
Maybe this helps
Thank you!
you might running directly welcome.php page.
Replace your welcome.php with
<html>
<body>
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { ?>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
<?php }
else{ ?>
<script> location.replace("yourHtmlFileName.html") </script>
<?php } ?>
</body>
</html>
The W3Schools page you linked has two examples using both POST and GET on the same page.
Considering you said you didn't edit the code, i think it's very likely you copied the HTML code of the second example that uses GET whiile using the PHP code from the first example that uses POST. The code block is right below is.
Frankly, this kind of thing happens to everyone sometimes, make sure you're properly hydrated and carry on!
Another issue which I faced for similar problem was that I have named my html file as "index.html" and also php file as "index.php" because of which php was working on php files first In my case changing the name of file worked out for me
try this php code in your index page:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
echo $name .'Welcome '. $lastname .'your email adress is: '. $email;
?>
As you said, if you literally copied and pasted a example from w3schools and got those errors, check if you're using an VSCode with live server plugin. I just stop using the live server plugin (Five server) and my problem with PHP was gone. For some reason, that plugin was the origin of ERROR: Undefined array key.
I am a little bit stuck trying to get a tracking code to work for my website.
Every click to my website through a tracker inserts a randomly generated unique string within the URL. For example, http://www.examplepage.com/?sub_ref=333ktcm1ckpv2uvd
When someone does goes through a step by step process on my website, a PHP script will load containing various variables, one of which is an external javascript:
$script = <script type="text/javascript" id="js1" src="https://www.example.com/load.php?id=8bb1ff8aa970aa5f018dcce821dc6251"></script>
In order for me to facilitate the tracking, I want to be able to add the unique string in the URL into the javascript. The idea is that I can track when someone clicks my website and then completes an entry with an external script. For example:
$script = <script type="text/javascript" id="js1" src="https://www.example.com/load.php?id=8bb1ff8aa970aa5f018dcce821dc6251?sub_ref=333ktcm1ckpv2uvd"></script>
Could someone advise the best way to achieve this? I've been playing around with this for most of the day without success.
I should add, at the point I want the unique ID added onto the script, the URL on my website is http://www.examplepage.com/?sub_ref=333ktcm1ckpv2uvd#submit-entry
Thank you #CFP Support that's really useful.
So based on your advice, I've managed to work out that using the below displays the correct URL string that I'm looking to add within the javascript.
https://example.com/test/test.php?sub_ref=230948324095
<?php
echo __LINE__ . " here, we look at _GET "; print_r($_GET); echo "<br>";?>
8 here, we look at _GET Array ( [sub_ref] => 230948324095 )
So based on the above, I believe I should be using:
<?php echo ($_GET['sub_ref'])?>"
The problem I've got now, is that if I use the following, I end up with an error:
$script = <script type="text/javascript" id="js1" src="https://www.example.com/load.php?id=8bb1ff8aa?&sub_ref=<?php echo ($_GET['sub_ref']); ?>"></script>
PHP message: PHP Parse error: syntax error, unexpected '<' in
/home/admin/web/exampledomain.com/public_html/settings.php
on line 13" while reading response header from upstream
EDIT:
This is where I am at currently. I've made the change you recommended (first one) which doesn't product an error, however the sub_ref in the button that loads the script is still blank.
settings.php file
<?php
// Set the referral network
$network = 'A';
// Set your submit code below.
$onClick_code = 'call_referral()';
// Set your the referral script below.
$script = '
<script type="text/javascript" id="js1" src="https://www.example.com/load.php?id=8bb1ff8aa?&sub_ref=' . $_GET['sub_ref'] . '"></script>';
?>
final-step.php
<?php
require_once '../settings.php';
?>
<h1>Submitting Data</h1>
<p class="second-paragraph">Please wait while we process your data.</p>
<div class="data-processing-wrapper">
<div class="data-processing-inner-wrapper">
<div class="cssload-loader-walk">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<span class="console-msg"></span>
<div class="data-verification-wrapper">
<h3>Data Verification</h3>
<p>Thank you, you are almost done submitting your data.
<br>Click on the Submit Now button below to submit your data.
</p>
<div class="button-wrapper data-verification-button-wrapper">
<a class="button data-verification-button"
<?php if ($network == 'A' || $network == 'B') {?> onclick="
<?php if (!empty($onClick_code)) { echo $onClick_code; } ?>"
<?php } ?>>Submit Now
</a>
<?php if ($network == 'A' && !empty($script)) { echo $script; } ?>
</div>
</div>
<div id="progressBarConsole" class="console-loadbar">
<div></div>
</div>
</div>
</div>
From inspecting the Submit Now button in Chrome, this is what it shows (the ?sub_ref is blank):
<div class="button-wrapper data-verification-button-wrapper">
<a class="button data-verification-button" onclick="call_referral()">Submit Now</a>
<script type="text/javascript" id="js1" src="https://www.example.com.net/test.php?id=8bb1ff8&sub_ref="></script> </div>
Am I correct in assuming that the sub_ref does not appear because the script is not being when the page is opened (only when final-step.php runs)?
Looks to me like you are close, just need to point to the right query....
src="https://www.example.com/load.php?id=8bb1ff8aa970aa5f018dcce821dc6251&sub_ref=<?php echo urlencode($_GET['sub_ref'])?>"
Looks fine (though from the example you gave you shouldn't need the urlencode).
It is really hard to tell what is going on though without some actual code.... (which is likely why nobody is answering you..... - nor can I give you much of an answer......)
However, as a 'troubleshooting' tip......
You have to know what you have as variables to work with at any given moment, so learn to stop the code at any given point and take a look what the code sees.
In this case, a good one would be to see what is in the $_SERVER array to see what links are really available before trying to build the reference link (this will tell you where you really are as well as how the user got to you, etc. - a wealth of info to be sure!)
echo __LINE__ . " here, we look at _SERVER "; print_r($_SERVER); echo "<br>";
Another good one at this point is the $_GET, which will tell you what is in the query string as an array....
echo __LINE__ . " here, we look at _GET "; print_r($_GET); echo "<br>";
Having the results of both those will tell you that you actually have the data you think you do (and that is quite often the reason things don't work like you expect!)
(you can also add a
die();
after any line to make the code stop..... Lots of ways to do this, and the most important part is just seeing what is going on!
Use the above, change your question to include more code and show what you are getting in $_SERVER and $_GET at that point and you will get some great, exact answers, I'm sure!
EDIT (after a bit more info.... - but still no full code..... :( again, seeing the script around all this would help get this done..... not sure why you aren't including it...)
Obviously you are in PHP at this moment (as it is a PHP error - the script would tell us that...) and your code won't work in PHP.
You have....
$script = <script type="text/javascript" id="js1" src="https://www.example.com/load.php?id=8bb1ff8aa?&sub_ref=<?php echo ($_GET['sub_ref']); ?>"></script>
and
PHP message: PHP Parse error: syntax error, unexpected '<' in
/home/admin/web/exampledomain.com/public_html/settings.php on line 13"
while reading response header from upstream
Which is telling you the issue - - - you can't write JS inside PHP! You can make it a variable, then use it later, or you could 'jump in/out' of PHP/HTML to do it, but you must remember what language you are in and respect that language's rules.
So, you could have:
$script = '<script type="text/javascript" id="js1" src="https://www.example.com/load.php?id=8bb1ff8aa?&sub_ref=' . $_GET['sub_ref'] . '"></script>';
Or, 'jump in/out' with:
// PHP code ......
// 'jump out'....
?>
<!-- now you are in HTML and can do some JS -->
<script type="text/javascript" id="js1" src="https://www.example.com/load.php?id=8bb1ff8aa?&sub_ref=<?php echo ($_GET['sub_ref']); ?>"></script>
<!-- where you had correctly 'jumped', but improperly mixed... -->
<!-- now, back to PHP... ->
<? // and you can do more PHP code here.....
You have to respect the language - and where you are at any point in the code!
If you still have issues, INCLUDE THE FULL CODE AROUND THIS (I'm sure it isn't rocket science gov't secret stuff, so save us all some time so you can get the help you are asking for, please!
EDIT:
in your settings.php the link shows as
src="https://www.example.com/load.php?id=8bb1ff8aa?&sub_ref=' . $_GET['sub_ref']
However, you say in the button it is
src="https://www.example.com.net/test.php?id=8bb1ff8&sub_ref="
Your code is not clear on how things are going...... (does this sound familiar? SHOW THE CODE AS IT IS PROCESSED..... - I can't {and now, won't be able to - I've put too much time on this and am getting 'looks'...} try to guess where that weird change came from. You need to show something that is logical.....
and, prove that you have the _GET - use the print_r just before the _GET to make sure you have the data as it is being processed (sometimes with PHP you have to do some 'tricks' to keep the data on one page and use it on another..... there are several ways to do it, but until I understand your flow and what you are trying to do overall {and mostly, why you are using so many pages.... - it could be an overall design/flow issue...}, it is really hard to get a picture of what you are trying to accomplish..)
SO is not really a training facility - and I have other projects (I'm allowed a bit of time each day to answer questions, etc. - part of the 'give back to the community' policy around here, but my primary work is on paid projects {I'm sure you understand...}, so I can't do more today, but if you give some clear 'steps' on what is going on and errors you see, etc. I can look at this again tomorrow.
Is there a way to get the screen size through php? I have had a look on google, and found a way of doing it through javascript as follows:
PHP :
<?php
$width = "<script>sw=screen.width;</script>";
$height = "<script>sh=screen.height;</script>";
echo "Width: ".$width." Height: ".$height;
?>
But this doesn't store anything in $width or $height. is this a good way of going about doing it? or is there another way?
You can't do that this way, here is a way to do it :
<?php
session_start();
if(isset($_POST['width'])){
$_SESSION['screen_size'] = array();
$_SESSION['screen_size']['width'] = intval($_POST['width']);
$_SESSION['screen_size']['height'] = intval($_POST['height']);
}
if(!isset($_SESSION['screen_size'])){
?>
<html>
<head>
<script>
function getSize(){
document.getElementById('inp_width').value=screen.width;
document.getElementById('inp_height').value=screen.height;
document.getElementById('form_size').submit();
}
</script>
</head>
<body onload='getSize()'>
<form method='post' id='form_size'>
<input type='hidden' name='width' id='inp_width'/>
<input type='hidden' name='height' id='inp_height'/>
</form>
</body>
</html>
<?php
}else{
var_dump($_SESSION['screen_size']);
}
This is a simple way, and the page will reload the first time.
You may want to use AJAX.
Also, if someone refuses sessions cookies, this would loop forever, better test if the browser accepts cookies.
You cannot get browser width by only using php. PHP is server code.
Try to look at this. You can use ajax to send (post) browser width in your php code.
Hope it helps.
So I'm trying to get my webpage to save some text to a server-side file. It's kinda like a guestbook, but I want it in a text file so I can review it before I put it on the site. I've been looking around, and from the looks of it, you can't do it with javascript because of 'security hazards'. I've seen things about php, but php files never seem to work for me. Is there a way to do this in plain .html and if not how do I make a php file do this, and be very specific with the steps required to make said php file. And I'm using apache if that helps at all. Thanks for your help.
This will take one PHP file and is not secure. But, it is what you want. The user can very easily sign the guest book. Additionally, it will display previous guest entries on the page. Yay!
guestBook.php
<?php
session_start();
if (!empty($_POST['name'])) {
if (!isset($_SESSION['posts'])) {
$_SESSION['posts'] = 0;
} else if ($_SESSION['posts'] >= 5) {
echo "<div style='color:red'>Failed to post - max posts exceded</div>";
} else {
$_SESSION['posts'] += 1;
file_put_contents("guest.txt",htmlspecialchars($_POST['name']) . "<br />",FILE_APPEND);
}
}
echo file_get_contents("guest.txt");
?>
<form method="post" action="">
<input name="name" type="text" value="John Kieth" />
<input type="submit" />
</form>
P.S. You literally can't do this through regular HTML/JavaScript.
I want to display my account only when user login into website and i am creating this into netsuite here is my account section html code
<div class="name" style="display:none;" id="
my_act">MY ACCOUNT</div>
I m using this script but its not working
<script type="text/javascript">
var $j = jQuery.noConflict();
$j(function()
{
if (entityId != null && entityId != "")
{
$j('#my_act').css('display','block');
}
});
</script>
I would say try putting an alert in the if loop to check whether the element exists at the time when the script is run.
Possibly, the script is gettig executed prior to the element actually getting loaded in the DOM.
First of all, you can't properly manage logins with javascript/jQuery alone. Logins are best managed with a back-end language, like PHP or ASP/.Net. This is because it is the server that must determine if someone has logged in, not their own browser.
I will use PHP as the server language for this example.
A great tool for keeping track of whether someone has been authenticated is PHP $_SESSIONs
<?php
session_start();
mysql_connect('localhost','my_cpanel_login_ID','my_cpanel_password') or die($connect_error); //or die(mysql_error());
mysql_select_db('cpanelLoginID_dbname') or die($connect_error);
if(isset($_SESSION['user_name'])===true) {
echo '<div id="my_act">Here is the information from their account</div>';
}else{
echo 'Login';
}
Here are some online video tutorials that may help:
PHPAcademy.org - used to be free, but is now just low-priced
Registration and Login tutorial
PHP and MySQL with PDO and mysqli_
TheNewBoston.com -- try this one:
Project Lisa - Unfinished tutorial but very good
Intro to PHP
Intro to jQuery
<html>
<head>
<script src="jquery.js"></script>
<script>
$j=jQuery.noConflict();
$j(document).ready(function()
{
$j("#my_act").show();
}
)
;
</script>
</head>
<body>
<div class="name" style="display:none" id="my_act">MY ACCOUNT</div>
</body>
</html>
Hope this helps !
This is not an answer, just an extended comment with some things (untested) for you to try:
Since entity has a value, you can try checking the length of the entityId. First, begin with an alert:
alert(entityId.length);
to ensure it does what we desire.
Then, something like:
if (entityId.length > 0) {
$j('#my_act').css('display','block');
}
BTW, are you sure that your jQuery.noConflict(); code is working correctly? Try something simple (without any if statements to add any additional layers of complexity):
$j('#my_act').css({'color':'red','font-size':'bold'});
Does that work?