• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[PHP] Onload?

Joined
Dec 15, 2009
Messages
1,387
Reaction score
236
I want the script that can send his details(ip....etc) to database when he's viewing the page instead of pressing the submit button.

EG:
1. Goto google.com
2. Google save your details automatically to their db when you loaded the page.

Mhm bad english....sorry for that...

Hoping anyone can solve my issue :)

After I ran a quick research on google, it seems that it requires Javascript to execute php script?
 
Last edited:
Newbie Spellweaver
Joined
Aug 22, 2008
Messages
88
Reaction score
14
You can put php anywhere in any of your pages and like ThuGie said, PHP executes the code directly so you dont need any submit form for that...

Just put PHP tags in the top of your home page...

Example :

<?php

do stuff here

<?

<html>
<head>
</head>
<body>
</body>
</hmtl>

.....
 
Joined
Aug 4, 2010
Messages
572
Reaction score
177
LOL, Those guys above got a good point x,x. I have no clue what I was talking about before.

Code:
<html>
<body>

//Anthing you want to automatically execute is below within the <?php <? tags.

<?php

//Code to execute would be placed here

$string = "This line of string is going to be automatically executed once the browser page as loaded.";
echo $string;


?>



</body>
</html>


In other hands if you we're trying to retrieve information from a html script well, then you would need a 'Submit' button for the info to be sent through the PHP script.
 
Joined
Nov 17, 2008
Messages
800
Reaction score
1,392
PHP already does everything "on load". All you need to do is find the details you want in the database, and put the code at the top of the page. (putting it at the top is only for convenience. It can be anywhere on the page.)

If you're wondering how to insert something into a mysql database with php, it's just a simple query.
Code:
<?php
mysql_query("INSERT INTO `table_name`(`online`,`other_column`) VALUES('1','other_value') WHERE `username` = 'user'") or die(mysql_error());

That query will insert '1' in the 'online' column of 'table_name', and 'other_Value' in the 'other_column' column of the same table.
 
Joined
Apr 28, 2005
Messages
6,953
Reaction score
2,420
Initialize a database connection, grab variables, insert to database...

PHP:
$con = mysqli_connect($host, $user, $pass, $db) or die("Failed to connect to database");
$username = $_SESSION['username'];
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "UPDATE users SET ip = '$ip' WHERE username = '$username';";
mysqli_query($con, $sql);
 
Joined
Dec 15, 2009
Messages
1,387
Reaction score
236
Initialize a database connection, grab variables, insert to database...

PHP:
$con = mysqli_connect($host, $user, $pass, $db) or die("Failed to connect to database");
$username = $_SESSION['username'];
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "UPDATE users SET ip = '$ip' WHERE username = '$username';";
mysqli_query($con, $sql);
Bro thanks but I know the basics
 
Back
Top