I am creating a simple enquiry form with upload option.
If I click submit button it goes to another page and displays success message but it should come in the same page as a message box. What should I do?
Here is my Html code:
<ul>
<li><span>Name</span>
<input name="name" type="text" size="25" class="text1" />
</li>
<li><span>Contact</span>
<input name="contact" type="text" size="25" class="text2" />
</li>
<li><span>E-mail</span>
<input name="email" type="text" size="25" class="text3" />
</li>
<li><span>Subject</span>
<input name="subject" type="text" size="25" class="text4" />
</li>
<li><span>Message</span>
<textarea name="message" size="250" class="text5"></textarea>
</li>
</ul>
<label for="upload"></label>
<div class="x">
<input type="file" name="upload" id="file" class="button">
</div>
<div class="x1">
<input type="submit" name="submit" value="Submit" class="button" />
</div>
And this is my PHP code for uploading and inserting:
<?php
$target = "upload/";
$target = $target . basename($_FILES['upload']['name']);
$ok = 1;
if (move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{
echo "The file " . basename($_FILES['upload']['name']) . " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
?>
<?php
include ("conn.php");
if (isset($_POST['submit']))
{
$sql="insert into cform (name,contact,email,subject,message,upload)values('".$_POST['name']."','".$_POST['contact']."','".$_ POST['email']."','".$_POST['subject']."','".$_POST['message']."','$target')";
$result=mysql_query($sql);
if (!$result)
{
echo "could not enter data";
}
else
{
echo "successfuly entered";
}
}
?>
This PHP page has to run in background and when I click the submit button a message box should display success.
Edit the echo portion of the page as
second page
$rspnse = "";
if(!$result){
$rspnse = "Error";
}
else{
$rspnse = "Success";
}
header('Location: previous_page.php?response='.$rspnse);
Replace the previous_page.php with your original url of the first page
first page
<?php
if(isset($_GET['response'])){
?>
<script>
alert('<?php echo $_GET['response'] ?>');
</script>
<?php
}
?>
Hope this helps...
if(! $result) {
$msg = "could not enter data";
}else{
$msg = "successfuly entered";
}
header('Location: http://www.example.com/form.php?message=$msg');
//Redirect to form page with set message.
add script in form page to display message
if(isset($_GET['message'])){
print $_GET['message'];
}
This solution for as per your code
And another option is using ajax.
Related
I want to implement a simple login with PHP. But, also I want to customize the error message when the login is incorrect, for example display a error message in red color.
Login.php
<form id="frmlogin" name="frmlogin" method="POST" action="validarUsuario.php">
<p><input type="text" name="usuario" id="usuario" class="required" maxlength="50" placeholder="Nombre Usuario"></p>
<p><input type="password" name="password" id="password" class="required" maxlength="50" placeholder="Password"></p>
<p id=error> </p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
validarUsuario.php
<?php
include("connection.php");
conectar_bd();
$usr = $_POST['usuario'];
$pw = md5($_POST['password']);
$sql = "SELECT usr FROM Client WHERE usr = '$usr'AND password ='$pw'";
$result = mysql_query($sql, $con);
if ($fila = mysql_fetch_array($result)) {
header('Location: menuclient.php');
} else {
/* How customize my message error */
}
?>
In the Login.php file, when the username or password are not correct, i want to display, for example, in id=error : "Login Incorrect" in red.
I dont know if I can do it with php.
Thanks for your help.
You can use session. Try following way.
Login.php
<?php
session_start();
if($_SESSON['error']): ?>
<span>Error : <?php echo $_SESSION['errorMsg']; ?></span>
<?php endif; ?>
<form id="frmlogin" name="frmlogin" method="POST" action="validarUsuario.php">
<p><input type="text" name="usuario" id="usuario" class="required" maxlength="50" placeholder="Nombre Usuario"></p>
<p><input type="password" name="password" id="password" class="required" maxlength="50" placeholder="Password"></p>
<p id=error> </p>
<p class="submit"><input type="submit" name="commit" value="Login"></p>
</form>
validarUsuario.php
<?php
session_start();
include("connection.php");
conectar_bd();
$usr = $_POST['usuario'];
$pw = md5($_POST['password']);
$sql = "SELECT usr FROM Client WHERE usr = '$usr'AND password ='$pw'";
$result=mysql_query($sql,$con);
if( $fila=mysql_fetch_array($result) )
{
header('Location: menuclient.php');
}
else {
$_SESSION['errorMsg'] = "Wrong username or Password";
$_SESSION['error'] = true;
header('Location: Login.php');
}
?>
I am trying to make a simple price match contact form on my product page in Magento. I have a separate php file but it doesn't seem to receive the commands from the view.phtml file.
this is the form code which is located inside /app/design/frontend/default/my_design/template/catalog/product/view.phtml
<div class="pricematcher">
<body class="body">
<div id="pricematch" style="display:none;">
<!-- Popup Div Starts Here -->
<div id="popupContact">
<!-- Contact Us Form -->
<form action="send_contact.php" id="form" method="post" name="form">
<img id="close" src="<?php echo $this->getSkinUrl(); ?>images/close.png" onclick="div_hide()"</img>
<h2 class="h2price">Price Match</h2>
<hr id="hrprice">
<input id="name" name="name" placeholder="Name" type="text">
<input id="email" name="email" placeholder="Email" type="text">
<input id="productname" name="productname" placeholder="<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?><?php echo $this->__(' PART #: ');?><?php echo $_helper->productAttribute($_product, nl2br($_product->getSku()), 'sku') ?>" type="text" readonly>
<input id="competitor" name="competitor" placeholder="Competitor Product Link" type="text">
<textarea id="msg" name="msg" placeholder="Message"></textarea>
Submit
</form>
</div>
<!-- Popup Div Ends Here -->
</div>
<!-- Display Popup Button -->
</body>
</div>
<img id="popup" src="<?php echo $this->getSkinUrl(); ?>images/price-match.png" onclick="div_show()"</img>
this is the javascript which makes the form pop up and display
// Validating Empty Field
function check_empty() {
if (document.getElementById('name').value == "" || document.getElementById('email').value == "" || document.getElementById('msg').value == "") {
alert("Fill All Fields !");
} else {
document.getElementById('form').submit();
alert("Thank You for submitting a Price Match inquiry");
}
}
//Function to Hide Price Match
function div_hide(){
document.getElementById('pricematch').style.display = "none";
}
//Function To Display Price Match
function div_show() {
document.getElementById('pricematch').style.display = "block";
}
this is the php file that the form action should send to send_contact.php file located in the same directory as view.phtml
<?php
$subject = "Price Match";
$message = $_POST['msg'];
$name = $_POST['name'];
$product = $_POST['productname'];
$competitor = $_POST['competitor'];
$mail_from = $_POST['email'];
// Enter Your email Adress
$to = "myemail#email.com";
$body = "$message, $competitor, $product";
$send_contact = mail($to,$subject,$body, "From: " . $mail_from);
//Check if message sent
if ($send_contact){
echo "we recieved";
}
else {
echo "error";
}
?>
Every thing works when I make this in a seperate folder on my server and execute outside of magento.
You should put send_contact.php at magento's root directory.
I think you can create an action in ProductController.php,for example,postEmailAction, then in the postEmailAction,you can get the data from the form:
$message = $_POST['msg'];
$name = $_POST['name'];
$product = $_POST['productname'];
$competitor = $_POST['competitor'];
$mail_from = $_POST['email'];
, flow the code:
`
$mail = Mage::getModel('core/email');
$mail->setToName('YourStore Admin');
$mail->setToEmail($toEmail);
$mail->setBody($body);
$mail->setSubject('YourStore: New Product Review');
$mail->setFromEmail('donotreply#yourstore.com');
$mail->setFromName("YourStore");
$mail->setType('html');
try {
$mail->send();
}
catch (Exception $e) {
Mage::logException($e);
}
`
the action url will be the product controller + the action name;
Hope this will help you.
I have a file upload form that I want to be submited automatically when the file has been selected. I am using Javascript onchange event to do it.
<form action="form.php" method="post" enctype="multipart/form-data">
<input type="file" name="filename" onchange="javascript:this.form.submit();">
</form>
This code seems to work as it is, but I have a PHP if statement that processes the file uploaded when a $_POST['submit'] happens (using the the submit button)
<?php
if(isset($_POST['submit'])){
$photo = Photograph::find_by_userid($user_id);
$photo = new Photograph();
$photo->attach_file($_FILES['photo_upload']);
}
?>
But since the point of this is to remove the submit button <input type="submit" name="submit"/> the PHP if code is not being triggered, ergo the file is not being uploaded. Do you know how can I make the if statement work when not using a submit button and using an auto submit with Javascript?
Thanks in advance!
This is the whole code..
<?php require_once("../includes/initialize.php"); ?>
<?php include_layout_template("header.php"); ?>
<?php include_layout_template("edit_header.php"); ?>
<?php
if(!$session->is_logged_in()){
redirect_to("login.php");
}else{
}
$id=$session->user_id;
$user=User::find_by_id($id);
$user_id = $user->id;
if(isset($_POST['submit'])){
$photo = Photograph::find_by_userid($user_id);
if($photo){
$photo->destroy();
$photo = new Photograph();
$photo->user_id = $user_id;
$photo->attach_file($_FILES['photo_upload']);
if($photo->save()){
redirect_to("edit_photo.php");
$session->message = "Photo uploaded succesfully";
} else{
$message = join("<br/>", $photo->errors);
}
}
$photo = new Photograph();
$photo->user_id = $user_id;
$photo->attach_file($_FILES['photo_upload']);
if($photo->save()){
redirect_to("edit_photo.php");
$session->message = "Photo uploaded succesfully";
} else{
$message = join("<br/>", $photo->errors);
}
}
$photo = Photograph::find_by_userid($user_id);
?>
<section class="edit_section">
<h2>Upload/Change <br/> Your Photo</h2>
<p><?php echo form_errors($errors); ?></p>
<p class="messages"><?php echo $message ?></p>
<form class="edit_form" id="form" action="edit_photo.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<ul>
<li>
<img src="<?php if(isset($photo)){echo $photo->image_path();} ?>" width="250px" />
<label for="photo_upload">Upload/Change <br/> your photo</label>
<input type="file" id="photo_upload" name="photo_upload" style="background:none; border:none;" value="upload" onchange="javascript:this.form.submit();"/>
<!--input type="submit" name="submit" value="Upload" /-->
</li>
</ul>
</form>
<section style="margin-left: 400px;" >
<p class="button">Delete it</p>
</section>
</section>
<?php include_layout_template("footer.php"); ?>
One solution is to add a hidden field to the form, this field will be recognized by PHP, for example, let's call it "my_flag" :
<form action="form.php" method="post" enctype="multipart/form-data">
<input type="file" name="filename" onchange="javascript:this.form.submit();">
<input type="text" name="my_flag" hidden />
</form>
Now, on the PHP side, you do :
<?php
if ( isset( $_POST['my_flag'] ) )
echo "flag";
?>
The echo "flag"; is just for testing, you replace it by your code.
I'm trying to integrating a PHP function to add the database into my file for tracing out purpose.
Here's my HTML code :-
<div id="" class="wf-formTpl">
<form accept-charset="utf-8" style="margin-top:;" action="https://app.getresponse.com/add_contact_webform.html?u=fgWk" method="post">
<input class="wf-input wf-req wf-valid__email" type="text" name="email"
data-placeholder="yes" value="Enter Your Email Here" onfocus=" if (this.value == 'Enter Your Email Here') { this.value = ''; }" onblur="if (this.value == '') { this.value='Enter Your Email Here';} " style="margin-top:;"></input>
<br />
<input type="submit" class="wf-button" name="submit" value="" style="display: inline !important; margin-top:-10px !important;"></input>
<input type="hidden" name="webform_id" value="6724404" />
</form>
</div>
<script type="text/javascript" src="http://app.getresponse.com/view_webform.js?wid=6724404&mg_param1=1&u=fgWk"></script>
Here's the PHP code :
<?php
$time = $_POST['email'];
if(isset($_POST['submit'])){
echo '<script type="text/javascript">alert("Done");</script>';
$myFile = "user.html";
$fh = fopen($myFile, 'w') or die("error");
$stringData = 'Notification'.$time;
fwrite($fh, $stringData);
};
?>
The PHP function doesn't go through , a.k.a the javascript function doesn't pops up.
I need a favor on how to make this happen :(
The function runs the HTML but not the PHP .
Thanks in advance.
i am working on one page ajax contactform . I want to send an email with PHP without leaving my page and now i'm completely lost. I have no idea why its not working
i'll show you my contact form. thank you for help in advance. Here is the form ..
html code
<article class="panel" style="background:url(img/cover/bg_panel_3.jpg); background-size:cover;" full-screen>
<div style="padding-top:80px;">
<div class="row text-center centered">
<div id="contact-form">
<form id="contact-us" action="contact.php" method="post">
<ul>
<li class="field" id="check-1">
<input class="input txt requiredField wide alpha-only" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" placeholder="Name" onBlur="check(this.value)" />
</li>
<li class="field" id="check-2">
<input class="input wide mail-only txt requiredField email" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" placeholder="Email" />
</li>
<li class="field" id="check-3">
<input class="input wide hack-check sub-check requiredField" name="subj" type="text" placeholder="Subject" value="<?php if(isset($_POST['subj'])) echo $_POST['subj'];?>" />
</li>
<li class="field" id="check-4">
<input class="input wide num-only requiredField" name="num" type="text" placeholder="Number" value="<?php if(isset($_POST['num'])) echo $_POST['num'];?>" />
</li>
<li class="field" id="check-5">
<textarea class="input textarea wide hack-check msg-check txtarea requiredField" name="comments" id="commentsText" class="txtarea requiredField" placeholder="Message:">
<?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?>
</textarea>
</li>
</ul>
<div class="medium primary btn pretty-btn">
<input name="submit" class="subbutton" type="submit" value="Send" />
</div>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
<?php } ?>
<!-- End #contact -->
</div>
</div>
</div>
</article>
**js code **
$(document).ready(function() {
$('form#contact-us').submit(function() {
$('form#contact-us .error').remove();
var hasError = false;
});
if(!hasError) {
var formInput = $(this).serialize();
alert(formInput);
$.post("contact-us.php",formInput, function(data){
$('form#contact-us').slideUp("fast", function() {
$(this).before('<p class="tick fg-white sans"><strong>Thanks!</strong> Your email has been delivered. !</p>' );
});
});
}
return false;
});
});
PHP Code
//If the form is submitted
if(isset($_POST['submitted'])) {
$name = trim($_POST['contactName']);
$email = trim($_POST['email']);
$num = trim($_POST['num']);
$subject = trim($_POST['subj']);
$comments = trim($_POST['comments']);
$formcontent=" From : $name \n Email : $email \n Subject : $subj \n Phone number : $number \n \n Message : $message";
$recipient = "example#mail.com";
$subject = "Contact Form Query Received";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error encountered! Please try again or write directly to the mentioned email address.");
// set our boolean completion value to TRUE
$emailSent = true;
}
I suspect your problem is this extra }); in your code which is stopping the $.post(...) call from even being called. A quick lint of your JS shows this up, as does careful reading.
var hasError = false;
======> }); <====== remove this extra stuff.
if(!hasError) {