How to call a controller withour going to the url in magento - php

I am new to magento. I needed to know if its possible to call a controller without going to url. What i want to do is to call the controller but staying on the same page. I have a form on the front end which accepts user input and than call a controller to save the data in the database by using models. Eveything works ok but my problem is that when user click on submit button the user goes to the controller url but i want to stay on the same page but call the controller. here is my code
<?php $_product = $this->getProduct(); ?>
<form id="test" action="<?php echo Mage::getUrl('pricenotify/pricenotify/db') ?>" method="post">
<label for="price">Price *</label>
<input type="text" id="price" name="price" value="" class="required-entry validate-number"/><br />
<label for="email">Email Address *</label>
<input type="text" id="email" name="email" value="" class="required-entry validate-email"/>
<input type="hidden" id="id" name="id" value="<?php echo $_product->getId() ?>" />
<input type="hidden" id="propri" name="propri" value="<?php echo $_product->getPrice() ?>" />
<input type="submit" name="submit" value="<?php echo $this->__('Submit') ?>" />
</form>
<script type="text/javascript">
//< ![CDATA[
var customForm = new VarienForm('test',true);
//]]>
</script>

The simplest way to convert to AJAX is to modify the button like this:
<input type="submit" name="submit" value="<?php echo $this->__('Submit') ?>"
onclick="if(customForm.validator && customForm.validator.validate()) this.form.request(); return false;" />
It doesn't do anything with the results, just calls the relevant controller/action.

Related

Wordpress Custom Login Form which stores the data as well as emails the field values

If the above title looks confusing then here is the description....
I have a template page where I have placed the wordpress default registration form. Now what exactly I want is to add few extra fields on that form.
The wordpress registration will go on as it is. I mean the username and email and password gets stored on the database but along with that new fields or extra details like phone/address/age etc etc gets emailed to a specific email id.
<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" class="wp-user-form">
<div class="username">
<label for="user_login"><?php _e('Username'); ?>: </label>
<input type="text" name="user_login" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="101" />
</div>
<div class="password">
<label for="user_email"><?php _e('Your Email'); ?>: </label>
<input type="text" name="user_email" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" id="user_email" tabindex="102" />
</div>
<div class="login_fields">
<?php do_action('register_form'); ?>
<input type="submit" name="user-submit" value="<?php _e('Sign up!'); ?>" class="user-submit" tabindex="103" />
<?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
<input type="hidden" name="user-cookie" value="1" />
</div>
</form>
**Note:- The new fields aren't added here.
Is it possible? If yes, then how? Should I add a second form below this form which fires the email? Kindly please suggest an appropriate solution for this.

PHP variable over POST with a formular

I want to send a variable via POST. The variable should not be in the URL like in this formular:
<form action="index.php?content=contact" method="POST">
<a class="article-input-a">Name:</a><br><input name="name" type="text" maxlength="30"><br>
<a class="article-input-a">E-Mail Adresse:</a><br><input name="email" type="text" maxlength="30" placeholder="beispiel#email.de"><br>
<a class="article-input-a">Website:</a><br><input name="website" type="text" maxlength="40" placeholder="www.beispiel-website.de"><br>
<button class="button" name="submit" type="submit">Beitrag erstellen</button>
</form>
In this formular i can send the variables, but i dont see them in the URL.
And i can get the variables over $_POST['name']. Now i want to send the variable $random with the formular submite button.
I hope you can undestand me.
To see them in your URL you need to use GET not POST.
<form action="index.php?content=contact" method="GET">
<a class="article-input-a">Name:</a><br><input name="name" type="text" maxlength="30"><br>
<a class="article-input-a">E-Mail Adresse:</a><br><input name="email" type="text" maxlength="30" placeholder="beispiel#email.de"><br>
<a class="article-input-a">Website:</a><br><input name="website" type="text" maxlength="40" placeholder="www.beispiel-website.de"><br>
<button class="button" name="submit" type="submit">Beitrag erstellen</button>
To send the variable, add
<input type=hidden name='random' value="<?php echo $random; ?>">
This will make a hidden variable that will be submitted.
Then access using $_GET['random']. But it will be in your URL as ?Name=&E-Mail=&Website=
if i understand correcty you want hidden inputs
<form action="index.php?content=contact" method="POST">
<a class="article-input-a">Name:</a><br><input name="name" type="text" maxlength="30"><br>
<a class="article-input-a">E-Mail Adresse:</a><br><input name="email" type="text" maxlength="30" placeholder="beispiel#email.de"><br>
<a class="article-input-a">Website:</a><br><input name="website" type="text" maxlength="40" placeholder="www.beispiel-website.de"><br>
<input type="hidden" name="content" value="contact" />
<input type="hidden" name="verName" value="varValue" />
<button class="button" name="submit" type="submit">Beitrag erstellen</button>
</form>
sending variables as part of the url is a GET request, not a POST request
then you access like
$_POST['varName']
to send that variable, you need to create a hidden field, that hold the value of that random number
<input type=hidden value="<?php echo $random; ?>">

multiple forms on a single PHP page

Im having issues with a ''profile'' page where users will be able to change their username, email, name, password and so on but it seems to have conflicts when i have more than 1 form on the page as they all work individually?
I could be missing something obvious so if anyone could help id much appreciate it.
Base
<?php
require 'core/init.php';
include 'includes/overall/header.php';
?>
<h1><p>Hello <?php echo escape($user->data()->username); ?></a>!</p></h1>
<h4><p>You joined the MMOunition community <?php echo escape($user->data()->joined); ?></a></p></h4>
<alert_banner>
<?php
if(Session::exists('home')) {
echo '<p>', Session::flash('home'), '</p>';
}
?>
</alert_banner>
<br />
<form action="changeusername.php" method="post">
<div class="field">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php echo escape($user->data()->username); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<br />
<form action="changepassword.php" method="post">
<div class="field">
<label for="password_current">Current password:</label>
<input type="password" name="password_current" id="password_current">
</div>
<div class="field">
<label for="password_new">New password:</label>
<input type="password" name="password_new" id="password_new">
</div>
<div class="field">
<label for="password_new_again">New password again:</label>
<input type="password" name="password_new_again" id="password_new_again">
<input type="submit" value="Change">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
</br>
<form action="changename.php" method="post">
<div class="field">
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="<?php echo escape($user->data()->name); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<br />
<form action="changeemail.php" method="post">
<div class="field">
<label for="email">Change email address:</label>
<input type="text" name="email" id="email" value="<?php echo escape($user->data()->email); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</div>
</form>
<?php
include 'includes/overall/footer.php';
?>
Hello You haven't mentioned what problem you actualy facing but this seems like the problem is the unique identification of a form at server side
Please see this post
Multiple HTML Forms on One Page
Hope this will help ;)
Have each form action set to the current page, and use hidden inputs to determine what action is being taken. Using this method will prevent the user from being dropped onto a different page, and puts the action being taken into a variable that you can use or manipulate.
If you want to have separate PHP scripts for actual functions, use includes or requires.
<form action="" method="post">
<div class="field">
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php echo escape($user->data()->username); ?>">
<input type="submit" value="Update">
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<input type="hidden" name="action" value="changeUsername">
</div>
</form>
Then the PHP script gets the action from the POST and uses a switch to determine which functions to call.
if (isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
switch ($action) {
case "changeUsername":
changeUsername();
break;
case "changePassword":
...
As mentioned, if you want to have these functions in separate php files, you can use include or require.
This conveniently drops the user right back on the same page when they update information, so that they can make additional updates.

Merge wordpress searchform code with custom html code

How do I make this code (custom):
<div id="sb-search" class="sb-search">
<form>
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="search" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
</div>
retrieve search results like this one ( wordpress ) does:
<form method="get" class="searchform" action="<?php echo home_url('/'); ?>">
<div>
<input type="text" class="search" name="s" onblur="if(this.value=='')this.value='<?php _e('To search type and hit enter','typegrid'); ?>';" onfocus="if(this.value=='<?php _e('To search type and hit enter','typegrid'); ?>')this.value='';" value="<?php _e('To search type and hit enter','typegrid'); ?>" />
</div>
Basically I got this custom code for an awesome search box with on-click slide etc. It has a field where people can type, but it gets no results as used on my wordpress site. I wanna make it able to get results from wordpress content w/o changing divs thus not altering its design. Please help, I'm stuck!!
This would do it:
<div id="sb-search" class="sb-search">
<form action="<?php echo home_url('/'); ?>">
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="s" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
</div>
All you have to do is add the form action, and make the name of the search input to be "s".
<form method="get" class="searchform" action="<?php echo home_url('/'); ?>" >
<input class="sb-search-input" placeholder="Search.." type="text" value="" name="s" id="search">
<input class="sb-search-submit" type="submit" value="">
<span class="sb-icon-search"></span>
</form>
homeurl ?s=anything will call wordpress search function, so just make form's action GET and ACTION = your home url

PHP validation resets the form fields

if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one

Categories