Redirecting from one CGI page to another - sql

Merged with How can I redirect the client from one CGI page to another using Perl?.
I am a nembie to CGI with Perl, please do help me out and please do correct me wherever I have committed a mistake.
This is the code. The problem is that after the password is validated and found as correct, instead of redirecting to the next page it is giving this message:
Status: 302 Found
Location: http://localhost/cgi-bin/Main.cgi
#!C:\perl\bin\perl.exe
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use strict;
use DBI;
use strict;
use warnings;
my $q = new CGI;
print "Content-Type: text/html\n\n";
if ($q->param("Login")) {
my $Password = param('Password');
if (!$Password) {
print "Please Enter the Password";
} else {
my $dbh = DBI->connect(
"dbi:SQLite:DEVICE.db",
"", "",
{
RaiseError => 1,
AutoCommit => 1
}
);
my $sth = $dbh->prepare("select * from Settings where Password = ?");
$sth->execute($Password);
if (my $pass = $sth->fetchrow_hashref) {
print redirect(-url => 'http://localhost/cgi-bin/Main.cgi');
} else {
print "Invalid Password";
display_form();
}
$dbh->disconnect;
}
}
if (($q->param("submit"))) {
process_form();
} else {
display_form();
}
sub process_form {
if (validate_form()) {
}
}
sub validate_form {
my $User_Password = $q->param("Password");
my $error_message = "";
$error_message .= "Please enter the Password<br/>" if (!$User_Password);
if ($error_message) {
display_form($error_message, $User_Password);
return 0;
}
}
sub display_form {
my $error_message = shift;
print <<END_HTML;
<html>
<head><title>Form Validation</title></head>
<body>
<form method="post">
<input type="hidden" name="submit" value="Submit">
<TABLE align="center" bgcolor=#B0C4DE>
<TR>
<TD> Enter The Password And Click Login</TD>
</TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR>
<TD><b>PASSWORD</b> : <input type="password" name="Password" size="20" maxlength="15" /></TD>
</TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR>
<TD align="center" colspan="2">
<input type="submit" name="Login" value="Login">
<input type="reset" name="Cancel" value="Cancel">
</TD>
</TR>
</TABLE
</form>
</body>
</html>
END_HTML
}

Related

get element id from template using smarty - Prestahop

I'm using Smarty in Prestashop. In my php file I built a function like this:
public function hookDisplayAdminOrderContentOrder($params)
{
$orderId = Tools::getValue('id_order'); // mi prendo l'id dell'ordine per le query sui docId
$query = 'SELECT docId1, docId1 FROM '._DB_PREFIX_.'orders WHERE id_order=\'' . $orderId . '\';';
$docId2 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docId2'];
$docId1 = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docId1'];
$config_CreaOrdine = Configuration::get('PS_CREAZIONE_ORDINE');
$config_CreaFattura = Configuration::get('PS_CREAZIONE_FATTURA');
// uso order_documents per il contatore nel template
$order_documents = 0;
if ($docId1 || $docId2)
$order_documents++;
else if ($docId1 && $docId2)
$order_documents = 2;
if(!$docId1){
$message_order = 'documento NON salvato';
$submit_button_id = 'submit_order';
$submit_label = "Salva Ordine";
}else{
$message_order = 'documento salvato';
}
if(!$docId2){
$message_invoice = 'documento NON salvato';
$submit_button_id = 'submit_invoice';
$submit_label = "Salva Fattura";
}else {
$message_invoice = 'documento salvato';
}
smartyRegisterFunction($this->context->smarty, 'function', 'saveDocument', array($this, 'smartyDocument')); // aggiungo la funzione per salvare di nuovo l'ordine
$this->context->smarty->assign('id_order', $orderId);
$this->context->smarty->assign('config_CreaOrdine', $config_CreaOrdine);
$this->context->smarty->assign('config_CreaFattura', $config_CreaFattura);
$this->context->smarty->assign('order_documents', $order_documents); // contatore documenti salvati
$this->context->smarty->assign('order', $message_order);
$this->context->smarty->assign('order_docId', $docId1); //docId per tasto ordine
$this->context->smarty->assign('invoice', $message_invoice);
$this->context->smarty->assign('invoice_docId', $docId2); //docId per tasto fattura
return $this->display(__FILE__, 'views/templates/hook/admin_content_order.tpl');
}
Then in my admin_content_order.tpl I set the button name and id like this:
<div class="tab-pane" id="myplugin">
<table>
<tbody>
<thead>
<tr>
<td style="padding-left: 20px;"><strong>{l s='Stato ordine' mod='myplugin'} </strong></td>
<td style="padding-left: 20px;"><strong>{l s='Stato fattura' mod='myplugin'} </strong></td>
</tr>
</thead>
<tr>
<td style="padding-left: 20px;">{$order}</td>
<td style="padding-left: 20px;">{$invoice}</td>
</tr>
<tr>
<td style="padding-left: 20px;">
{if $order_docId == '' && $config_CreaOrdine eq 1}
<button type="submit" name="submit_order" id="submit_order" class="btn btn-primary" onclick={saveDocument}>Salva Ordine</button>
{/if}
</td>
<td style="padding-left: 20px;">
{if $invoice_docId == '' && !$config_CreaFattura eq 0}
<button type="submit" name="submit_invoice" id="submit_invoice" class="btn btn-primary" onclick={saveDocument}">Salva Fattura</button>
{/if}
</td>
</tr>
</tbody>
</table>
</div>
At last in my php file I define the function "smartyDocument". I wish a slight different behaviour of it, based on the button id. Anyone knows how I can I get the button id and pass it to "smartyDocument" function? I hope is all clear, thx
You cannot use any Smarty commands after loading the page. You can use the Javascript function instead.
Smarty is a server-side engine and the output is sent to the browser after processing, which is quite static.
You have to have a Javascript function to save your document by Ajax:
send your data to PHP
save data
return success or fail message

Cant write data in database using codeigniter

I have a controller, model and view file. I am trying to write data to database using the form on view file. I am trying to validate the post data using a mix of Codeigniter Validation library and some methods defined by. This validation is being done in Controller. Then I am trying to pass data array to the model. In the model I am trying to read the data array and build a query to insert data in database.
The problem is that no data is being written in the database.
I dont see any visible errors in the browser. I have been stuck on this for some time now. Any help is appreciated. Thanks in advance.
Controller
function add_customer() {
$this->load->model('Customer_Model');
$data['title'] = "Add New Customer";
$this->load->view('templates/header' , $data);
$this->load->view('dashboard/add_customer' , $data);
$this->load->view('templates/footer' , $data);
if($this->input->post())
{
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|customer_email_exists');
$this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|customer_mobile_exists');
$this->form_validation->set_rules('address', 'Address', 'trim|required');
if ($this->form_validation->run()){
$customer_data = array(
'name' => $this->validation->name,
'email' => $this->validation->email,
'mobile' => $this->validation->mobile,
'address' => $this->validation->address
);
$this->Customer_Model->add_customer($customer_data);
}else{
}
}
}
public function customer_email_exists($email) {
$this->load->model('Customer_Model');
if(!$this->Customer_Model->email_exists($email)){
return true;
}else{
$this->form_validation->set_message('email_exists', 'Email already registered, try another one.');
return false;
}
}
public function customer_mobile_exists($mobile) {
$this->load->model('Customer_Model');
if(!$this->Customer_Model->mobile_exists($mobile)){
return true;
}else{
$this->form_validation->set_message('email_exists', 'Email already registered, try another one.');
return false;
}
}
}
Model
class Customer_Model extends CI_Model{
function add_customer($customer_data)
{
$data = array(
'id'=>'',
'name'=>$customer_data["name"],
'email'=>$customer_data["email"],
'mobile'=>$customer_data["mobile"],
'address'=>$customer_data["address"]
);
$this->db->insert('customer',$data);
$this->db->query($query);
}
public function email_exists($email) {
$this->db->where("email = '$email' AND email != ''");
$query = $this->db->get('customer');
if ($query->num_rows() > 0){
return true;
}else{
return false;
}
}
public function mobile_exists($mobile) {
$this->db->where('mobile',$mobile);
$query = $this->db->get('customer');
if ($query->num_rows() > 0){
return true;
}else{
return false;
}
}}
View
<section class="versir-section">
<div class="container">
<div class="row">
<div class="col-12">
<?php echo validation_errors(); ?>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">Customer Name</td>
<td width="329"><input type="text" name="name"/></td>
</tr>
<tr>
<td>Customer Email </td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>Customer Mobile </td>
<td><input type="text" name="mobile"/></td>
</tr>
<tr>
<td>Customer Address </td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="save" value="Save Data"/></td>
</tr>
</table>
</form>
</div>
</div>
</div>
Hmm! I think in this code part you should get post data from input, not from validation
if ($this->form_validation->run()){
$customer_data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'address' => $this->input->post('address'),
);
$this->Customer_Model->add_customer($customer_data);
}else{
}

Php 7 error for a simple form getting Object of class mysqli could not be converted to int

The code gets error saying Object of class mysqli could not be converted to int. When i try to load the page i get this error,i checked other questions but it was not helpful
<?php
$conn = mysqli_connect("localhost", "root", "", "coinlion");
if ($conn == 0) {
echo "could not connect";
} else {
mysqli_select_db("coinlion");
echo "connected";
}
?>
<form method="post">
<style>
#newlink {
width: 600px
}
</style>
<div id="newlink">
<div>
<table border=0>
<tr>
<td> Link URL:</td>
<td><input type="text" name="linkurl" value="" required></td>
</tr>
<tr>
<td> Link Description:</td>
<td><textarea name="linkdesc" cols="50" rows="5" required></textarea>
</td>
</tr>
</table>
</div>
</div>
<p>
<br>
<input type="submit" name="submit1">
<input type="reset" name="reset1">
</p>
php code
<?php
if (isset($_POST['submit1'])) {
$linkurl = $_POST['linkurl'];
$linkdesc = $_POST['linkdesc'];
mysqli_query($conn, "insert into
test(linkurl,linkdesc)values('$_POST[linkurl]','$_POST[linkdesc]')");
echo "data inserted";
}
?>
Well the issue was with the line if($conn==0) ,This value was taken as an integer so the error occured and an Connection variable to Mysqli_select_db was missing , after fixing it , the code ran successfully

How do I include html in C# block and C# in html block in the following View?

I have my view here and it throws errors, either there is missing {} block or an "eternal components throws and exception" when I remove those if/else block along with #: the code works just like before so how do I include html code in a C# block and C# code in html block in the following view:
#model IEnumerable<ecomm2.Models.HomeSearchResultsViewModel>
#{
if (Model.Count < 1)
{
#:<p style="color:red">Item not found</p>
}
else
{
#:<table style="background-color:#f7f7f7;width:100%; border:0px solid black;">
foreach (var item in Model) {
#:<tr style="border:1px solid #bbb9b9;">
#: <td style="width:177px;">
#: <img src="~/Content/images/meter.jpeg" alt="Alternate Text" style="height:177px;width:177px;padding:10px;"/>
#:</td>
#:<td style="width:100%;padding-left:2px;float:left;padding-top:20px;border:0px solid black;font-size:medium">
<span>
#Html.ActionLink(item.ProductLineName, "GetProductDetails", "Product", new { id = item.Id }, new { }) <br />
#: </span>
#:<span style="font-size:small">By #Html.ActionLink(item.BrandName, "GetProductByBrandName", new { id=item.BrandName})</span><br />
#:<span style="font-size:x-small">
#Html.ActionLink(item.CategoryName, "GetProductsByCategoryName", new { id=item.CategoryName}) | Stock Count: #Html.DisplayFor(modelItem => item.StockCount)
</span><br />
#using(Html.BeginForm("AddToCart", "Cart", new { id=item.Id}))
{
<fieldset>
<input type="submit" name="name" value="Add to Cart" class="btn btn-default"/>
</fieldset>
}
</td>
<td style="width:20%;color:#00b02f;font-weight:bold;padding-top:20px;float:none;padding-left:20px;">
LKR: #Html.DisplayFor(modelItem => item.ListPrice)
</td>
</tr>
}
#:</table>
}
}
Here is the original code that threw the error
External component has thrown an exception.
Here is the original code
#model IEnumerable<ecomm2.Models.HomeSearchResultsViewModel>
#if (Model.Count < 1)
{
<p style="color:red">Item not found</p>
}
else
{
<table style="background-color:#f7f7f7;width:100%; border:0px solid black;">
#foreach (var item in Model) {
<tr style="border:1px solid #bbb9b9;">
<td style="width:177px;">
<img src="~/Content/images/meter.jpeg" alt="Alternate Text" style="height:177px;width:177px;padding:10px;"/>
</td>
<td style="width:100%;padding-left:2px;float:left;padding-top:20px;border:0px solid black;font-size:medium">
<span>
#Html.ActionLink(item.ProductLineName, "GetProductDetails", "Product", new { id = item.Id }, new { }) <br />
</span>
<span style="font-size:small">By #Html.ActionLink(item.BrandName, "GetProductByBrandName", new { id=item.BrandName})</span><br />
<span style="font-size:x-small">
#Html.ActionLink(item.CategoryName, "GetProductsByCategoryName", new { id=item.CategoryName}) | Stock Count: #Html.DisplayFor(modelItem => item.StockCount)
</span><br />
#using(Html.BeginForm("AddToCart", "Cart", new { id=item.Id}))
{
<fieldset>
<input type="submit" name="name" value="Add to Cart" class="btn btn-default"/>
</fieldset>
}
</td>
<td style="width:20%;color:#00b02f;font-weight:bold;padding-top:20px;float:none;padding-left:20px;">
LKR: #Html.DisplayFor(modelItem => item.ListPrice)
</td>
</tr>
}
</table>
}
In the Index view it calls the partial page "_SearchResultList" here is the video that shows all
None of your #: code is necessary. The reason you get the exception is that IEnumerable<T> does not have a property Count so your if block throws an exception. Change you code to
if (Model.Count() < 1)
of better, use
if (Model.Any())
and swap the code in the if and else blocks

loging php page not redirecting

I have a login.php page created with Dreamweaver CS4.
After inputting username and password in the form the page reloads itself not redirecting to desire location.
URL for existing website: http://www.decoraflooringstore.com
login page opens after clicking on client login
Below is the php code:
Thanks for any input
<?php require_once('Connections/test.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_test, $test);
$query_Recordset1 = "SELECT `user`.`user name`, `user`.password FROM `user`";
$Recordset1 = mysql_query($query_Recordset1, $test) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
if (isset($_POST['username'])) {
$loginUsername=$_POST['username'];
$password=$_POST['username'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "success login.php";
$MM_redirectLoginFailed = "failed login.php";
$MM_redirecttoReferrer = false;
mysql_select_db($database_test, $test);
$LoginRS__query=sprintf("SELECT `user name`, password FROM `user` WHERE `user name`=%s AND password=%s",
GetSQLValueString($loginUsername, "-1"), GetSQLValueString($password, "text"));
$LoginRS = mysql_query($LoginRS__query, $test) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = "";
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;
if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
<table width="500" border="0" align="center">
<tr>
<td><table width="400" border="0" align="center">
<tr>
<td>Username</td>
<td><form id="form1" name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
<label>
<input type="text" name="username" id="username" />
</label>
</form></td>
</tr>
<tr>
<td>Password</td>
<td><form id="form2" name="form2" method="post" action="">
<label>
<input type="text" name="password" id="password" />
</label>
</form></td>
</tr>
<tr>
<td> </td>
<td><form id="form3" name="form3" method="post" action="">
<label>
<input type="submit" name="login" id="login" value="Login" />
</label>
</form></td>
</tr>
</table></td>
</tr>
</table>
<?php
mysql_free_result($Recordset1);
?>
In order to be able to redirect, you must avoid sending previous contents. Otherwise you will receive the message "headers already sent".
The command session_start() will send headers, not allowing further redirection.
Avoid also having multiple opening and closing php tags. You are sending newline chars between the closing tags and the next opening tags. This will also cause headers to be sent, not allowing the redirection to work as expected.
use isset function for checking user name and password .and i in the conditional statement
if passsword is right
header("location:index.php");
else
header("location:something.php");
what it does it if your information is valid it will redirect to index.php and if not valid it will redirect to something.php