I am trying to get my page to redirect after deleting a record from the database but it wont redirect. It is echoing out text and also deleting the record but it just reloads me on the same page with a form that no longer has information in it.
Here is the button im using
echo '<form>';
echo '<br /><br /><input style="cursor:pointer" class="button" type="submit" value="Delete Event" name="delete" id="delete" />';
echo '</form>';
Here is the PHP im using to delete the row from the DB
if (isset($_POST['delete'])){
$mysqli->query('DELETE FROM information WHERE id = '.intval($_GET['id']));
header('Location: index.php');
}
I have no idea whats wrong with it.
The header doesn't work, probably because there's already another one.
I suggest you to use a little part of Javascript:
if (isset($_POST['delete'])){
$mysqli->query('DELETE FROM information WHERE id = '.intval($_GET['id']));
echo "<script type='text/javascript'>window.location.href = 'index.php'</script>";
}
I assume that both the code snippets are present in a single PHP file. Then, your form will by default post to the same file using GET method.
Change your PHP code to look like this:-
<?php
if (isset($_GET['delete'])){
$mysqli->query('DELETE FROM information WHERE id ='.intval($_GET['id']));
header('Location: index.php');
}
echo '<form>';
echo '<br /><br /><input style="cursor:pointer" class="button" type="submit" value="Delete Event" name="delete" id="delete" />';
echo '</form>';
?>
Your form tag is completely empty. You check if $_POST["delete"] is set, but you haven't even specified the method of communication between your form and your PHP file on your form. Try changing your starting form tag to this:
<form method = "POST" action = "YOUR PHP FILE.php">
Your PHP file seems fine as long as index.php is on the same level as the form.
You can also redirect your page to somewhere else this way:
echo "<meta http-equiv="refresh" content="1;url=index.php"/>"
<form>
Delete id <input type = "text" name ="deleteid" id ="deleteid"/>
<input type ="submit" name = "submit" />
</form>
<?php
if (isset($_POST['submit'])){
$id = $_POST['deleteid'];
$query = "DELETE FROM information WHERE id = $id";
//rest Execute the query
header('Location: xyx.php');
}
<?
Related
I am using one session variable in my php page. As per my infomation, it is accessible throughout the program and it is, but problem is that it is showing different value for the same variable at different place in php page?
the code is as follows
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form>
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
value becomes different before and after submit button click? Please tell me why it is so?
thanks in advavnce.
You are redeclaring the value of session variable 'x' here
$_SESSION['x'] = $_SESSION['x']+1;
This is why its appearing 1 greater than its initial value.
it is due to the code itself
if(isset($_SESSION['x'])) //It is set
$_SESSION['x'] = $_SESSION['x']+1; //Add 1 to the value
echo $_SESSION['x']."<br>"; return value with +1
Solution
The reason the output is different is the order you echo and update
//Echo
//Update Value
//Echo again
Simple solution would be to move this
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
to above this
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
Also note set the method and the action in the form to make sure it calls itself
<form method="GET" action="[url to itself]">
<input type="submit" name="save" value="save" />
</form>
Do it like this :
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form method="GET" action="">
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
this way the code prints out the same value after submit as it did before.
Either way you try if you print value and change after or change value and print after, when page reloads it will change value. you could add another button called increment and add the following code inside the php :
if (isset($_GET['inc']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
}
and this one inside the form:
<input type="submit" name="inc" value="inc" />
this way youre variable increment when you press the inc button
I have Two forms in my index.php and am trying to get them to submit and POST their data seperatly
Form 1
echo '<form action="process.php?type=news" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Add News to News Feed';
echo '</th></tr><tr><td>';
echo 'Title:<br /><input name="newstitle" type="text" id="add" style="height:16px;width:525px;" size="80" maxlength="186" /><br />';
echo 'Main Body Text:<br /><textarea name="newsfeed" id="add" style="width:525px;height:78px;" maxlength="2000" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';
And Form 2
echo '<form action="process.php?type=suggest" method="post">';
echo '<table cellspacing="0"><tr><th>';
echo 'Suggest Additions to the Intranet';
echo '</th></tr><tr><td>';
echo '<textarea name="suggest" id="add" style="width:330px;height:60px;" maxlength="800" ></textarea>';
echo '<input style="float:right;margin-top:5px;" id="button" type="submit" value="Submit" />';
echo '</td></tr></table></from>';
I want these both to post and do the action after pressing the submit button, but currently the second form submit to the first forms action
How can i fix this???
EDIT: Also i am using .PHP for both the index and process page then using it to echo the forms onto the page
Also here is the process.php data
$type=$_REQUEST['type'];
$suggest=$_POST['suggest'];
$newstitle=$_POST['newstitle'];
$news=mysql_real_escape_string($_POST['newsfeed']);
if ($type == "news")
{
$sql=mysql_query("SELECT * FROM newsfeed WHERE title = ('$newstitle')");
$number_of_rows = mysql_num_rows($sql);
if ($number_of_rows > 0)
{
echo 'This Title is Already Taken';
}
else
{
$sql="INSERT INTO newsfeed (title, news) VALUES ('$newstitle','$news')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
exit;
}
}
elseif ($type == "suggest")
{
$sql="INSERT INTO suggestions VALUES ('$suggest')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header('Location: index.php');
exit;
}
Could it be that you are not closing the form with </form>, but with </from>, so the code of the second form is in fact still inside the first form since the tags are never closed?
try giving the submit an on click for each form so in its submit buttons html put this:
onclick="document.forms["myform1"].submit();"
and for form 2 put
onclick="document.forms["myform2"].submit();"
Im not sure exactly what you would like to achieve, so heres my assumptions,
1. your form submits wrongly, eg, second form submits the first form's data
or
2. you would like to do both submits 1 after another.
FOR 1:
Do this.
<form id="form1" action="process.php" method="POST">
<input type="hidden" name="type" value="news">
....
</form>
Note the spelling
/form not /from
<form id="form2" action="process.php" method="POST">
<input type="hidden" name="type" value="suggest">
....
</form>
Note the spelling
/form not /from
Some pointers:
Instead of echoing out so many times, echoing out once is suffice! Remember echos slow down your script. Also I would not pass the type as a $_GET, post is always safer for forms, you can use a hidden field to add the type:
<?php
echo '
<form action="process.php" method="post">
<input type="hidden" name="type" value="news">
...
...
</form>';//Not </from>
?>
Then with the PHP part use a switch case instead of if elses, it will be easier to add features then splitting with another ifelse, plus the default block can be used for anything that dont match like the last else in an if else statement.
<?php
$type = $_POST['type'];
switch($type){
case "news":
...
break;
case "suggest":
...
break;
default:
//default if different type or no type is set
break;
}
?>
Also move over to PDO or prepared mysqli_ functions for your mySQL stuff its safer.
I have a PHP generated form which consists of a list of items, each with a button next to them saying "Remove This" it outputs similar to below:
Item A - [Remove This]
Item B - [Remove This]
...
I wish to be able to click Remove This and it will detect which item it is, and then remove that from the database. Here's my code so far:
selectPlaces.php
<?php
include 'data.php';
mysql_connect($host, $user, $pass) or die ("Wrong Information");
mysql_select_db($db) or die("Wrong Database");
$result = mysql_query("SELECT * FROM reseller_addresses") or die ("Broken Query");
while($row = mysql_fetch_array($result)){
$placeName = stripslashes($row['b_name']);
$placeCode = stripslashes($row['b_code']);
$placeTown = stripslashes($row['b_town']);
$outputPlaces .= "<strong>$letter:</strong> $placeName, $placeTown, $placeCode <input type=\"button\" onclick=\"removePlace()\" value=\"Remove This\" /><br />";
}
mysql_close();
?>
Coupled with my admin.php
<div id="content" style="display:none">
Remove a Place<br><br>
<?php include 'selectPlaces.php'; echo $outputPlaces; ?>
</div>
I know I need to add some javascript to detect which button is clicked but I can't seem to get it working. I tried modifying the onclick="removePlace()" by perhaps passing a variable in the function removePlace(placeID) or something like that, but I'm new to JavaScript and I have no idea how to receive this in the removePlace function.
This seems easier to do without JavaScript. For each entry instead of generating just a button, generate a form that posts to a PHP script that does the deleting.
<form action="deletePlace.php?id=<?php echo $idOfThePlace?>">
<input type="submit" value="Remove This" />
</form>
$idOfThePlace would be the ID with you use to identify the data row.
You don't need JavaScript for that. Try running this example:
<?php var_dump($_POST); ?>
<form action="post.php" method="post">
<p>
<input type="submit" value="a" name="action" />
<input type="submit" value="b" name="action" />
</p>
</form>
You'll see that $_POST['action'] will depend on which button was pressed. For your example, you just need to set the value to identify the item that needs to be deleted. It might be useful to use the <button> element for that: <button name="delete" type="submit" value="12345">delete item 12345</button>. It'll show up as $_POST['delete'] with 12345 as value when submitted.
I would like to know how can I add a button to enable user to register the available domain name? My idea is to a button linked to registration page next to the available domain name. When the user click the button, the name of the searched domain will be passed to the next page, which is the registration page.
I've added the following code, but it's not working.
function showDomainResult($domain,$server,$findText){
if ($this->tr == 0){
$this->tr = 1;
$class = " class='tr2'";
} else {
$this->tr = 0;
$class = "";
}
if ($this->checkDomain($domain,$server,$findText)){
echo "<tr $class><td>$domain</td><td>$this->daftar_tld($domain)</td></tr>";
}
else echo "<tr $class><td>$domain</td><td class='tak'>TAKEN</td></tr>";
}
// To display register button
function daftar_tld($domain){
?>
<form method=post action="http://www.com">
<center><input value=$_POST["$domain"]><input type="submit" name="butangDaftar" class="sbtn" value="Register" /></center>
</form>
<?php
}
?>
And the output that I get is this (image is provided at the link below):
http://lh5.ggpht.com/_d7N0eqyUoUw/TGgPvCt4B3I/AAAAAAAAAHY/T7e4KjVNT04/Screen%20shot%202010-08-14%20at%2012.53.59%20PM.png
I really hope that someone can help me. I've been working on this for days. Phewwww...
You haven't set a name or type for the input variable holding the domain:
Your code:
<center><input value=$_POST["$domain"]><input type="submit" name="butangDaftar" class="sbtn" value="Register" /></center>
Suggested code:
<center><input type="hidden" name="domainToRegister" value="<?php echo $_POST["$domain"]; ?>" /><input type="submit" name="butangDaftar" class="sbtn" value="Register" /></center>
The form will then submit the domain as $_POST['domainToRegister'].
Trying to perform a very simple task here.
I have an <ol> that contains 4 rows of data in some handy <li>s. I want to add a delete button to remove the row from the table. The script in delete.php appears to have finished, but the row is never removed when I go back and check dashboard.php and PHPMyAdmin for the listing.
Here's the code for the delete button (inside PHP):
Print "<form action=delete.php method=POST><input name=".$info['ID']." type=hidden><input type=submit name=submit value=Remove></form>";
Moving on to delete.php:
<?
//initilize PHP
if($_POST['submit']) //If submit is hit
{
//then connect as user
//change user and password to your mySQL name and password
mysql_connect("mysql.***.com","***","***") or die(mysql_error());
//select which database you want to edit
mysql_select_db("shpdb") or die(mysql_error());
//convert all the posts to variables:
$id = $_POST['ID'];
$result=mysql_query("DELETE FROM savannah WHERE ID='$id'") or die(mysql_error());
//confirm
echo "Patient removed. <a href=dashboard.php>Return to Dashboard</a>";
}
?>
Database is: shpdb
Table is: savannah
Ideas?
It's refusing to stick because you're calling it one thing and getting it with another. Change:
"<input name=".$info['ID']." type=hidden>"
to
"<input name=ID value=".$info['ID']." type=hidden>"
because in delete.php you're trying to access it with:
$id = $_POST['ID'];
You should really quote attribute values as well ie:
print <<<END
form action="delete.php" method="post">
<input type="hidden" name="ID" value="$info[ID]">
<input type="submit" name="submit" value="Remove">
</form>
END;
or even:
?>
form action="delete.php" method="post">
<input type="hidden" name="ID" value="<?php echo $info['ID'] ?>">
<input type="submit" name="submit" value="Remove">
</form>
<?
Please, for the love of the web, don't built an SQL query yourself. Use PDO.
Just another point I'd like to make. I'm 95% sure that you can't give an input a numeric name/id attribute. It has to be like "id_1" not "1".
Also with php you can do arrays.
So you could do this
<input name="delete[2]">
then in your php
if(isset($_POST['delete']))
foreach($_POST['delete'] as $key=>$val)
if($_POST['delete'][$key]) delete from table where id = $val