I'm looking for a way to clean my DB with a button on a html-page (.php). I have a code that's working OK, but every time I go in to the page the function is cleaning my DB without me pushing the button.
Here is my code
<button id="checkoutbutton" onclick="cleanDb()">Clean DB</button>
<script>
function cleanDb()
{alert("<?php clean();?>")}
</script>
<?php
function clean()
{
$con=mysql_connect("localhost","rss","Habb0") or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db("kalender",$con) or die("Failed to connect to MySQL: " . mysql_error());
$sql='DELETE FROM `bilder` WHERE `stop` < now()';
mysql_query($sql);
echo "Databasen är rensad från gamla bilder";}
?>
Please help me!
PHP is parsed on the server side, JavaScript is parsed on the client side. Any PHP operation will run first, before any javascript (Not considering AJAX).
Use this code, but I am not using javascript. In your case you should consider AJAX. But 2nd option is as follow
<form action="" method="post">
<button id="checkoutbutton" onclick="cleanDb()" name="clebtn">Clean DB</button>
</form>
<?php
if(isset($_POST['clebtn']))
{
function clean()
{
// your code
}
}
?>
you must call ajax for it like this
<button id="checkoutbutton" onclick="cleanDb()">Clean DB</button>
<div id="result_content"></div>
<script>
function cleanDb()
{
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var result = xmlhttp.responseText;
// do your task for result
}
}
var url="clean_db.php";
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
and a PHP File clean_db.php
<?php
clean();
echo "db has been cleaned";
function clean()
{
$con=mysql_connect("localhost","rss","Habb0") or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db("kalender",$con) or die("Failed to connect to MySQL: " . mysql_error());
$sql='DELETE FROM `bilder` WHERE `stop` < now()';
mysql_query($sql);
echo "Databasen är rensad från gamla bilder";}
?>
Related
I'm sorry if this question may sound stupid, but i have one problem with my code that i don't know how solve even though i think it really easy for other people.
I'm making some form when user enter their info (example: username) then the output will shown in the option field (example:responsibility). For your information every username have many responsibility.
The problem is the output is creating many option field and not only one.
below is my php code that related with option field:
<?php
// 3.php is my config file
require("3.php");
$link = mysqli_connect($h,$u,$p,$db);
//if ($link)
//echo "Connection OK";
//else echo "connection failed";
$q = $_GET['q'];
$query="SELECT resp_name FROM view_ss_user_responsibility WHERE user_name ='".$q."'";
$result = mysqli_query($link,$query)
or die("Error: ".mysqli_error($link));
while ($row = mysqli_fetch_array($result)){
$fn=$row[0];
echo "<br>";
//echo "<td>$row[0]</td>";
echo "<tr><td>Responsibility:</td>
<td align=center><form><select><option value='$row[0]'>$fn</option></select></form></td></tr>";
}
echo "</table>";
?>
And also this is the output (i'm using ajax, so no submit button)
if you want to see my html/javascript file:
<html>
<head>
<script>
function showResult(str) {
if (str.length==0) {
document.getElementById("search").innerHTML="";
document.getElementById("search").style.border="0px";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("search").innerHTML=this.responseText;
document.getElementById("search").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
username <input type="text" size="30" name ='pass' onkeyup="showResult(this.value)"><br>
<div id="search"></div>
</form>
</body>
</html>
so, base from this i hope someone can tell me on how to do it, thank you.
(why is this getting downvote?.its not like i'm forcing you all to answer it)
Hope it will work fine..
Change this to:
while ($row = mysqli_fetch_array($result))
{
$fn = $row[0];
echo "<br>";
//echo "<td>$row[0]</td>";
echo "<tr><td>Responsibility:</td>
<td align=center><form><select><option value='$row[0]'>$fn</option></select></form></td></tr>";
}
echo "</table>";
This:
echo "<table><tr><td>Responsibility:</td><td><select>";
while ($row = mysqli_fetch_array($result))
{
$fn = $row[0];
echo "<option value='$row[0]'>$fn</option>";
}
echo "</select></td></tr></table>";
So I'm coding a banlist for one of my game servers. I'm using javascript to load the separate PHP page's mysql query, using an ActiveXObject.
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (str==0){
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
if(!IsNumeric(str)){
type = "name";
}
else
{
type = "id";
}
xmlhttp.open("GET","getdata.php?" + type +"="+str,true);
xmlhttp.send();
}
That handles the getting of the user's data. I also have a PHP script, on the same page, allowing users to add get requests to the page, to get the user's ban/bans quickly. That is handled by
$id = $_GET['id'];
$name = $_GET['name'];
if(isset($id)){
echo '<script> showUser('.$id.') </script>';
}elseif(isset($name)){
echo '<script> showUser('. $name .') </script>';
}
My problem comes when I attempt to use the page's name get.
When I browse to my site,
http://banlist.unityroleplay.com/minecraft/index.php?name=test
I am faced with the error of
ReferenceError: test is not defined
Any Ideas?
if(isset($id)){
echo '<script> showUser('.$id.') </script>';
}elseif(isset($name)){
echo '<script> showUser('. $name .') </script>';
}
this code is trying execute
<script>showUser(test)</script>
and javascript understanding it as test variable. But you must do it as
<script>showUser("test")</script>
So, you must change your code as follows:
if(isset($id)){
echo '<script> showUser("'.$id.'") </script>';
}elseif(isset($name)){
echo '<script> showUser("'. $name .'") </script>';
}
The variable is passed through ajax and it is a database name. When the link is clicked it's supposed to retrieve a data from the the database. the link and the variable is on the same page. Here is the code for the link:
$x = strval($_GET['x']);
echo ''.$seatid.'';
The $x variable contains the table name of the database. And here is the code for ajax:
function showInformation(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtInfo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getinfo.php?x="+str,true);
xmlhttp.send();
}
Here is the getinfo.php:
<?php
session_start();
$_SESSION['login']="1";
$x = strval($_GET['x']);
$con = mysql_connect('localhost','root','Newpass123#','seatmapping');
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db('seatmapping');
$sql="SELECT name, seatid FROM $x WHERE seatid = 1";
$result = mysql_query($sql) or die("Query Error " . mysql_error());
...
...
?>
I can't get it to work when i click the link it does not display the data from the table.
Please help me. Any kind of help is appreciated. Thanks in advance..
Can you please try this:
Replace the line
echo ''.$seatid.'';
With the below:
echo ''.$seatid.'';
This will solve your problem.
You are not passing the ID of the expected element.
echo ''.$seatid.'';
Replace this line, instead
echo ''.$seatid.'';
I have the following Code in cat.php file:
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getsub.php?q="+str,true);
xmlhttp.send();
}
</script>
<select name="users" onchange="showUser(this.value)">
<option value=""></option>
<?php
$query = "SELECT cat from category GROUP BY cat";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
$num=mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row=mysql_fetch_assoc($result);
echo "<option value=''>".$row['cat']."</option>";
}
echo "</select>";
<div id="txtHint"></div>
And after Calling the function - getsub.php
<?php
require_once('connect_db.php');
//get the q parameter from URL
$q=$_GET["q"];
$query = "SELECT sub from category WHERE cat = '".$q."'";
$result = mysql_query($query);
if(!$result){
mysqli_error();
}
while ($row = mysql_fetch_assoc($result)){
?>
<select name="sub">
<?php
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
echo "<option value='".$row['sub']."'>".$row['sub']."</option>";
}
?>
<select>
<?php
}
I'm looking forward to have a relation between Main Category and Sub Category. I want to Select the Main category and then calling the javascript function in order to show another select input for the Sub Category . The above code outputs nothing and I can't seem to find why?
Please Help
Your code looks fine but you have just a php tag in the end of the while loop that needs to be closed.
Code looks fine. Try using the firebug extension for Firefox.
Open net panel in firebug and then see what data are you sending and what response in actually generating.
This will definitely help.
Also in cat.php, instead of using plain AJAX, try using jQuery as this will reduce large line of code as
// Just incude jQuery beforehand
script>
function showUser(str)
{
$.get("getsub.php",{q:str},function(data){
$("#txtHint).html(data);
});
}
</script>
I'm trying to get the code below working so that it will call a JS function to pass a value to my PHP file which will return a number of values from a MySQL database. This is part of an assignment I thought would be quite straightforward but I think my problem is with the JavaScript event handler - how to reference the input value maybe?
The HTML:
<form>
<input type="text" name="users" onkeydown="showUser(this)"/>
</form>
<div id="txtHint">
<p id="responder"><b>Person info will be listed here.</b></p>
</div>
The showUser() function:
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("responder").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("responder").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","php/student_query.php?q="+str,true);
xmlhttp.send();
}
</script>
The PHP:
<?php
$q=$_GET["q"];
// Step 1
$conn = mysql_connect("localhost", "root");
// Step 2
mysql_select_db("collegeData", $conn);
//Step 3
$sql="SELECT * FROM studenttable WHERE studentID = '".$q."'";
$result = mysql_query($sql);
// Step 4
while ($row = mysql_fetch_array($result))
{
// Step 5
echo "Hello $row[firstName] $row[lastName]<br/>";
echo "Your two course modules are $row[moduleNo1] and $row[moduleNo2]<br/>";
echo "<tr><td> $row[firstName]</td><td> $row[lastName] </td> <td> $row[studentID] </td> </tr>";
echo "<br/>";
}
// Step 6
mysql_close($conn);
?>
Like I said, i think my problem is in the event handler, I'm not great with JS. I'd appreciate any help.
Looks like you're sending the input element to your function, not it's value. Try
<input type="text" name="users" onkeydown="showUser(this.value);" />
Also, you should protect your database query from protection by changing your PHP to
$q = mysql_real_escape_string(trim($_GET["q"]));
if($q == "")
{
echo "";
exit;
}