Hello There, Guest!
View New Posts  |  View Today's Posts
Resetting textbox in php?

  • 0 Vote(s) - 0 Average


01-01-2015, 12:25 PM #1
william7
Member
**
Posts: 205 Threads:49 Joined: Feb 2012 Reputation: 0

Resetting textbox in php?
Can anybody tell me what line of code I need to insert in the php script below that will clear the email textbox of data after it has been submitted and entered in the database? When I return to the page by the back button the email that's been entered is still there. I've tried reset, redirect, and a cleaning the string code. None work. I'm very new to php.

PHP Code:
<?php

define
('DB_NAME''myemaildata');
define('DB_USER''will777');
define('DB_PASSWORD''D241777');
define('DB_HOST''localhost');

$link mysql_connect(DB_HOSTDB_USERDB_PASSWORD);

if (!
$link) {
        die(
'Could not connect: ' mysql_error());

}

$db_selected mysql_select_db(DB_NAME$link);

if (!
$db_selected) {
    die(
'Can\'t use ' DB_NAME ': ' mysql_error());

}



$value $_POST['Email'];

$sql "INSERT INTO EmailList (Email) VALUES ('$value')";

if (!
mysql_query($sql)) {
       die(
'Error: ' mysql_error());

}

mysql_close();
?>

01-01-2015, 03:51 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Resetting textbox in php?
This won't help, this is only for form POST action submission. Where is your HTML code? You need to clear the value attribute for the text element. You should also have those defines in a separate file and included wherever you need them.
This post was last modified: 01-01-2015, 03:53 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

01-01-2015, 04:05 PM #3
william7
Member
**
Posts: 205 Threads:49 Joined: Feb 2012 Reputation: 0

RE: Resetting textbox in php?
Below is the form I used Ace. Are you suggesting that I could clear the textbox in the form somehow? I remember VB how easy it was to clear a textbox. I have a feeling it is much more difficult in php.

Code:
<form action="database.php" method="post" />
            <p>Submit your email address here to receive my blog posts by email: <input type="text" name="Email" /><input type="submit" value="Submit" /></p>
            
            </form>

01-01-2015, 04:12 PM #4
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Resetting textbox in php?
(01-01-2015, 04:05 PM)william7 Wrote:  Below is the form I used Ace. Are you suggesting that I could clear the textbox in the form somehow? I remember VB how easy it was to clear a textbox. I have a feeling it is much more difficult in php.

Code:
<form action="database.php" method="post" />
            <p>Submit your email address here to receive my blog posts by email: <input type="text" name="Email" /><input type="submit" value="Submit" /></p>
            
            </form>

The problem here is probably with your browser then. Do you have autofill enabled? Try a different browser or get rid of autofill. Read this: http://stackoverflow.com/questions/58224...-html-form

There's no other valid reason here as to why it should be automatically populated. This is a browser issue.
This post was last modified: 01-01-2015, 04:13 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

01-01-2015, 05:56 PM #5
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Resetting textbox in php?
I can't replicate your issue, have you tried:

Code:
header("Location: yourfile.php");

Also you can use jQuery to perform AJAX-requests pretty easily so there wouldn't even be a page-reload.

01-01-2015, 06:46 PM #6
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Resetting textbox in php?
Additionally, as pointed out in another thread mysql_?? functions are depreciated. You should be using mysqli_?? functions instead if you intend on using MySQL


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

01-01-2015, 07:13 PM #7
Epixors
(づ。◕‿‿◕。)づ・。*。✧・゜゜・。
*****
TLF Coders
Posts: 420 Threads:58 Joined: Aug 2012 Reputation: 6

RE: Resetting textbox in php?
In case someone is interested, here's a somewhat old database class, I haven't completely went over it in terms of security so make sure your input isn't anything unexpected hehe.

[code=php]

<?php

/*===========================*\
|| Database Class ||
|| Author: Epixors ||
|| Last Modified: 7-6-2014 ||
|| ||
\*===========================*/


class Database{

#region Properties
protected $_db;

protected $_where;
protected $_query;
protected $_orderBy;
protected $_limit;
protected $_prefix;
protected $_offset;
public $_directions = array("ASC","DESC"); //Allowed directions

#endregion

public function __construct($host,$user,$pass,$port,$database, $prefix = null){

//New connection or DIE IN FLAMES
$this->_db = new mysqli($host, $user, $pass, $database, $port)
or die('Oh noez! DB error!');

//Set default charset
$this->_db->set_charset('utf8');

$this->_prefix = $prefix;
}

public function close(){
$this->_db->close();
}

//Needed for prepared statements
//Returns the type of a variable
private function getType($obj)
{
switch (gettype($obj)) {
case 'NULL':
case 'string':
return 's';
break;

case 'integer':
return 'i';
break;

case 'blob':
return 'b';
break;

case 'double':
return 'd';
break;
}
return '';
}


//Set the where clause
public function where($clause){
$this->_where = $clause;
}

//Escapes
public function escape($var){
return $this->_db->real_escape_string($var);
}

//Set the order ($orders = array('name-ASC', 'id-DESC') )
public function orderBy($orders){
$this->_orderBy = "ORDER BY ";
foreach($orders as $order){
$order = explode("-", $order);
if(in_array($order[1],$this->_directions)){
$this->_orderBy .= $order[0]." ".$order[1].",";
}
}

//Remove last comma
$this->_orderBy = substr($this->_orderBy, 0, -1);
}

//Set limit
public function limit($limit){
$this->_limit = $limit;
}

//Set offset
public function offset($offset){
$this->_offset = $offset;
}


public function get($table,$columns = "*"){

//Check if it requests everything or a specified set
$columns = is_array($columns) ? implode(', ', $columns) : $columns;

//Build the base query string

$this->_query = "SELECT " . $columns . " FROM ". $this->_prefix.$table;
$this->constructQuery();
return($this->execute('get'));


}

public function count($table,$column='id'){
$this->_query = "SELECT COUNT(".$column.") FROM " . $table;
$this->constructQuery();
return($this->execute('count'));
}

public function delete($table){
$this->_query = "DELETE FROM ".$table;
$this->constructQuery();
return($this->execute('delete'));
}

//Insert function ('users' , array('id','username')
public function insert($table,$values){

$columns = array_keys($values);
$data = array_values($values);
$data_clean = array();

foreach($data as $piece){

if ($this->getType($piece) == 's'){
$data_clean[] = "'".$piece."'";
} else {
$data_clean[] = $piece;
}
}

$columns = implode(",",$columns);
$data_clean = implode(",",$data_clean);

$this->_query = "INSERT INTO " . $this->_prefix.$table . " (".$columns.") VALUES (". $data_clean . ")";
$this->constructQuery();
return($this->execute('insert'));
}


//Update function
public function update($table,$values){
$columns = array_keys($values);
$data = array_values($values);
$data_clean = array();

foreach($data as $piece){

if ($this->getType($piece) == 's'){
$data_clean[] = "'".$piece."'";
} else {
$data_clean[] = $piece;
}
}

$max = count($columns);
$sets = "";

for($i = 0;$i < $max;$i++){
$sets .= $columns[$i]." = ".$data_clean[$i] . ",";

}

$sets = substr($sets,0,-1);

$this->_query = "UPDATE " . $this->_prefix.$table . " SET ".$sets;
$this->constructQuery();
return($this->execute('update'));

}


public function execute($type){

switch(strtoupper($type)){
case 'GET':
if($stmt = $this->_db->prepare($this->_query)){
$stmt->execute();
$meta = $stmt->result_metadata();

while ($field = $meta->fetch_field()) {
$parameters[] = &$row[$field->name];
}


call_user_func_array(array($stmt, 'bind_result'), $parameters);

while ($stmt->fetch()) {
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}

$stmt->close();
$this->reset();

if(isset($results)){
return $results;
}


} else {
return false;
}
break;
case 'INSERT':
if($stmt = $this->_db->prepare($this->_query)){
$result = $stmt->execute();
$rows = $stmt->affected_rows;

$stmt->close();
$this->reset();

if($rows > 0){
return $rows;
} else {
return false;
}
}

break;
case 'COUNT':

if($stmt = $this->_db->prepare($this->_query)){
$result = $stmt->execute();
$result = null;
$rows = 0;
$stmt->bind_result($result);

while($stmt->fetch()){
$rows = $result;
}

$stmt->close();
$this->reset();

if($rows > 0){
return $rows;
} else {
return false;
}
}


break;
case 'UPDATE':
if($stmt = $this->_db->prepare($this->_query)){
$result = $stmt->execute();
$rows = $stmt->affected_rows;

$stmt->close();
$this->reset();

if($rows > 0){
return true;
} else {
return false;
}
} else {
return false;
}
break;
case 'DELETE':
if($stmt = $this->_db->prepare($this->_query)){
$result = $stmt->execute();
$rows = $stmt->affected_rows;

$stmt->close();
$this->reset();

if($rows > 0){
return true;
} else {
return false;
}
} else {
return false;
}
break;
default:
echo "This type of query is not supported yet.";
break;

}

}

//Set everything back after executing a query again
public function reset(){
$this->_where = null;
$this->_query = null;
$this->_orderBy = null;
$this->_limit = null;
}

public function constructQuery(){

$base = $this->_query;

if(!empty($this->_where)){
$base .= ' '.$this->_where;
}

if(!empty($this->_orderBy)){
$base .= ' '.$this->_orderBy;
}

if(!empty($this->_limit)){
$base .= ' LIMIT '.$this->_limit;
}

if(!empty($this->_offset)){
$base .= " OFFSET ". $this->_offset;
}

$this->_query = $base;

}
}

?>

[/code]

01-01-2015, 07:37 PM #8
william7
Member
**
Posts: 205 Threads:49 Joined: Feb 2012 Reputation: 0

RE: Resetting textbox in php?
You guys might not understand what my problem is. Go to http://greendrinksforschools.info/contact/index.html and submit an email. Then hit the back button and you'll see that the email is still in the textbox even though it was successfully submitted. The examples I saw on you tube didn't have this problem so I'm not sure what's happening.

01-01-2015, 10:36 PM #9
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: Resetting textbox in php?
I think you're not understanding something here actually. That's expected behavior... That's not an issue, that should be what happens. If you go back, the browser is going to take you to the last state it seen. The email is successfully submitted. If you navigate back to /contact manually, the email won't be there.

Essentially you're trying to find a solution to a problem which you've invented from the idea of expected browser behavior. It's nothing to worry about. :)

If you don't believe me, type any text in the quick reply below, hit the back button, then hit the forward button in your browser. For me as a firefox user, that text remains in tact.
This post was last modified: 01-01-2015, 10:38 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

01-02-2015, 02:57 AM #10
Mazzn
ლ(ಠ益ಠლ)
*******
Administrators
Posts: 199 Threads:16 Joined: Sep 2013 Reputation: 19

RE: Resetting textbox in php?
A quick suggestion on how to get around this:
Simply put a button or link labelled "back" leading to /contact/index.html on the confirmation page. This way the user doesn't have to (and probably won't) use the browser's back button and won't see the text in the textbox when returning.
Visit me at mazzn.net & blog.mazzn.net!
//This is very important :)

Self.KeepImproving(true);





Forum Jump:



Users browsing this thread: 1 Guest(s)