I was trying to push with a single form using the $_POST method multiple times the same form (with different values) until the array index is 4 (or a number I decide).
So with this html form i want to press submit, push the values into array, then refresh page, reinsert different values and so on until the condition is true.
<?php
if (!isset($count)) {
$person = array(
array(
'name' => '',
'surname' => '',
'phoneNumber' => ''
)
);
}
var_dump($person);
if (!isset($_POST['submit'])) {
echo "<form action=\"index.php\" method=\"post\">
<label for=\"name\">Name</label>
<input type=\"text\" name=\"name\" required>
<br>
<label for=\"surname\">Surname</label>
<input type=\"text\" name=\"surname\" required>
<br>
<label for=\"phoneNumber\">Phone Number</label>
<input type=\"text\" name=\"phoneNumber\" required>
<br>
<input type=\"submit\" value=\"Submit\" name=\"submit\">
</form>";
$count = 0;
} else {
$name = $_POST['name'];
$surname = $_POST['surname'];
$phoneNumber = $_POST['phoneNumber'];
if (count($person) <= 2) {
array_push($person, $name, $surname, $phoneNumber);
//echo "<meta http-equiv=\"refresh\" content=\"0\">";
//echo "Sto inserendo persone";
//echo count($persone);
echo count($person);
//var_dump($persone);
//print_r($persone);
} else {
var_dump($person);
};
}
?>
I was thinking about using $_SESSION but I don't have an idea about how to use it.
I don't want to use AJAX or jQuery or Javascript only pure PHP.
The example below shows always the form and your actual persons array. If you submit the form, your data will add to the array until it counts more than three entries. I think that is what you are looking for:
<?php
session_start();
if (!isset($_SESSION['persons'])) {
$_SESSION['persons'] = [];
}
if(isset($_POST['submit'])) {
if (count($_SESSION['persons']) <= 2) {
$_SESSION['persons'][] = array(
'name' => $_POST['name'],
'surname' => $_POST['surname'],
'phoneNumber' => $_POST['phoneNumber']
);
}
}
?>
<pre><?php var_dump($_SESSION['persons']); ?></pre>
<form action="index.php" method="post">
<label for="name">Name</label>
<input type="text" name="name" required><br>
<label for="surname">Surname</label>
<input type="text" name="surname" required><br>
<label for="phoneNumber">Phone Number</label>
<input type="text" name="phoneNumber" required><br>
<input type="submit" value="Submit" name="submit">
</form>
With the following line of code you can clear you array if you need to:
$_SESSION['persons'] = [];
Like in topic. How can I call the overrider controller from html form like this below:
<form method="post" action="index.php?controller=CmsController">
<input name="test" type="text" />
<input name="action" value="getStatus" type="hidden" />
<input value="test" type="submit" />
</form>
This code above I have in my tpl file.
File: /override/controllers/front/CmsController.php
---edit
{$link->getModuleLink('blockcms', 'CmsController')|escape:'html'}
doesn’t work.
---edit
public function initContent() {
parent::initContent();
if (Tools::getValue('action') && Tools::getValue('action') == 'getStatus') {
$id = $this->getIdByCode(Tools::getValue('voucher'));
$obj = new CartRuleCore($id);
$context = Context::getContext();
$response = $obj->checkValidity($context);
if ($response == NULL or $response == 'Koszyk jest pusty'){
$response = 'Kod ';
}
}
if (isset($this->cms)) {
$this->cms->content = str_replace("<!--response-->", $response, "{$this->cms->content}");
}
$this->setTemplate(_PS_THEME_DIR_ . 'cms.tpl');
This is part of initContent function in \override\controllers\front\CmsController.php And in my file tpl I have
<form method="post" action="index.php?controller=CmsController">
<input name="test" type="text" />
<input name="action" value="getStatus" type="hidden" />
<input value="test" type="submit" />
You have requested an outdated version of PayPal. This error often
results from the use of bookmarks.
I get this error when I login via a buyer account in sandbox mode.
my view file:
<html>
<body>
<form method="post" action ="https://www.sandbox.paypal.com/cgi-bin/webscr">
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="return" value="<?php echo $this->config->item('returnurl');?>" />
<input type="hidden" name="cmd" value=" " />
<input type="hidden" name="business" value="<?php echo $this->config->item('business');?>" />
<!--product 1-->
<input type="hidden" name="item_name_1" value="prod 1" />
<input type="hidden" name="item_number_1" value="p1" />
<input type="hidden" name="amount_1" value="2" />
<input type="hidden" name="quantity_1" value="1" />
<input type="submit" name="paypalbtn" value="buy with paypal">
</form>
</body>
</html>
my config file : paypal.php
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
$config['authtoken']='IDENTITY_TOKEN';
$config['posturl']='https://www.sandbox.paypal.com/cgi-bin/webscr';
$config['business']='DUMMY_FACILITATOR_EMAIL_ID';
$config['returnurl']='http://localhost/events/event_pay/success/';
$config['cancel_return']='http://localhost/events/event_pay/pay_fail';
?>
my controller file:
<?php
class event_pay extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('mdl_pay');
}
function index()
{
$this->load->view('demo');
}
function showplans()
{
$this->load->view('vw_header');
$this->load->view('vw_eventplans');
$this->load->view('vw_footer');
}
function check_events_by_user()
{
$u_id=37;
$numberofrows =$this->mdl_pay->has_paid($u_id);
echo $numberofrows;
}
function pay_success($data)
{
echo "return url function";
$this->load->view('success',$data);
}
function pay_fail()
{
echo "payment failed";
}
function success()
{
$res = $this->verifyWithPayPal($_GET['tx']);
$this->load->view('success_pay',$res);
}
function successdemo()
{
$this->load->view('vw_success_pay');
}
public function verifyWithPayPal($tx)
{
$token = $this->config->item('authtoken');
$paypal_url = $this->config->item('posturl').'?cmd=_notify-synch&tx='. $tx.'&at='.$token;
$curl= curl_init($paypal_url);
$data=array(
"cmd"=>"_notify-synch",
"tx"=>$tx,
"at"=>$token
);
$data_string=json_encode($data);
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$headers= array(
'Content-Type:application/x-www-form-urlencoded',
'Host: www.sandbox.paypal.com',
'Connection: close'
);
curl_setopt($curl,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
$response= curl_exec($curl);
$lines= explode("\n", $response);
$keyarray = array();
if(strcmp($lines[0],"SUCCESS")==0){
for($i=1;$i<count($lines)-1; $i++){
list($key,$val)=explode("=",$lines[$i]);
$keyarray[urldecode($key)]=urldecode($val);
}
$this->getListProducts($keyarray);
}
}
public function getListProducts($result)
{
$i=1;
$data = array();
foreach($result as $key => $value)
{
if(0===strpos($key,'item_number')){
$product = array(
'buyer_firstname' => $result['first_name'],
'buyer_lastname' => $result['last_name'],
'buyer_street' => $result['address_street'],
'buyer_city' => $result['address_city'],
'buyer_zip' => $result['address_zip'],
'buyer_state' => $result['address_state'],
'buyer_country' => $result['address_country'],
'buyer_country_code' => $result['address_country_code'],
'buyer_address_status' => $result['address_status'],
'buyer_pp_email' => $result['payer_email'],
'receiver_pp_email' => $result['receiver_email'],
'transaction_id' => $result['txn_id'],
'transaction_type' => $result['txn_type'],
'buy_date' => $result['payment_date'],
'buyer_pp_id' => $result['payer_id'],
//'address_name' => $result['address_name'],
'receiver_pp_id' => $result['receiver_id'],
'receiver_pp_email' => $result['receiver_email'],
'itemnumber' => $result['item_number'],
'itemname' => $result['item_name'],
'itemquantity' => $result['quantity'],
'mc_currency' => $result['mc_currency'],
'mc_fee' => $result['mc_fee'],
'mc_gross' => $result['mc_gross'],
'payment_gross' => $result['payment_gross'] ,
'paypal_pay_time' => date('Y-m-d H:i:s'),
);
$this->mdl_pay->insert_record($product);
echo "<script>alert('Payment Successful!')</script>";
}
}
echo "return statement:at end of for loop ";
return $product;
}
}
?>
The problem was occuring due to this line:
<input type="hidden" name="cmd" value="" />
I replaced it with:
<input type="hidden" name="cmd" value="_xclick" />
Read: https://github.com/websharks/s2member/issues/826 , if this doesn't work contact paypal support, there's not much we can do here.
Below is the code I use to process payment in my site. This cart has only one item. This code is for paypal. I want to sell same item on payza.
//process order for paypal
// Prepare GET data
$query = array();
$query['notify_url'] = 'http://mywebsite.com/ipn.php';
$query['cmd'] = '_cart';
$query['upload'] = '1';
$query['business'] = 'payment#mywebsite.com';
//main item
$query['item_name_1'] = $item_name;
$query['item_number_1'] = '1';
$query['amount_1'] = $item_price;
$query['currency_code'] = "USD";
// Prepare query string
$query_string = http_build_query($query);
header('Location: https://paypal.com/cgi-bin/webscr?' . $query_string);
<form method="post" action="https://secure.payza.com/checkout">
<input type="image" name="Checkout" src="btn_buynow.gif" />
<input type="hidden" name="ap_merchant" value="payza_email"/>
<input type="hidden" name="ap_purchasetype" value="item"/>
<input type="hidden" name="ap_itemname" value="SITENAME"/>
<input type="hidden" name="ap_itemcode" value="row->id"/>
<input type="hidden" name="ap_description" value="item1_desc"/>
<input type="hidden" name="ap_amount" value="amt"/>
<input type="hidden" name="ap_currency" value="CURRENCY_CODE"/>
<input type="hidden" name="ap_returnurl" value="returnurl"/>
<input type="hidden" name="ap_cancelurl" value="cancelurl"/>
</form>
I'm going to keep it short and simple. I'm writing a really basic code in php which adds content to mysql db and I've run into an issue with editing. This is my form:
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<div name="id"> '. $id . '</div>
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
And this is my editIt.php
//session start and stuff
if(filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING) == "POST")
{
echo "<script type='text/javascript'>alert('EDITIT!');</script>";
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("WebSiteDB") or die ("Cannot connect to database");
$id = $_POST['id'];
$details = mysql_real_escape_string($_POST['details']);
$time = strftime("%X");
$date = strftime("%B %d, %Y");
$isPublic = 'no';
foreach($_POST['public'] as $eachCheck)
{
if($eachCheck != NULL)
$isPublic = "yes";
}
mysql_query("UPDATE list SET details='$details', dateEdited='$date', timeEdited= '$time', public='$isPublic' WHERE id='$id'");
header("location: home.php");
}
I can't really find an issue with this code (which is not really strange, I'm a newbie at web stuff) and yet it just goes to home.php and does not change data in DB. Please, help me jump this ledge, so I can go on with my life.
I think, the problem is in this line $id = $_POST['id'];. On form submit, the input field value will only be submitted, not the DIV value.
So, please change from :
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<div name="id"> '. $id . '</div>
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}
To :
if($idExists)
{
Print '
<form action="editIt.php" method="POST">
<input type="hidden" name="id" value="' . $id . '">
Enter new detail: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Update List"/>
</form>
';
}