Show an error after submitting - php

I want to show an error (in phpBB it's trigger_error) right after the user clicks submit.
I already have a function which checks if a link has been posted. Now I want to call it after the submit post button has been clicked.
Where do I find that? Probably in posting.php?

Open posting.php.
find around line 631
if ($submit || $preview || $refresh)
{
after add
$post_data['your_url'] = "http://www.damienkeitel.com/index.php"; //remove the equals and url value if using in real post
$your_url = $post_data['your_url'];
$your_url_exists = (isset($your_url)) ? true : false;
$your_url = preg_replace(array('#&\#46;#','#&\#58;#','/\[(.*?)\]/'), array('.',':',''), $your_url);
if ($your_url_exists && http_file_exists($your_url) == true)
{
trigger_error('yeah, its reachable');
}
else if ($your_url_exists && http_file_exists($your_url) == false)
{
trigger_error('what da hell..');
}
open functions_posting
find around line 19
/**
* Fill smiley templates (or just the variables) with smilies, either in a window or inline
*/
after add
function http_file_exists($url)
{
$f = #fopen($url,"r");
if($f)
{
fclose($f);
return true;
}
return false;
}

Related

$_SESSION variable getting lost somewhere........or PHP ignoring "IF...ELSEIF"

I am having a problem with getting my PHP script to correctly read and execute my "IF.....ELSEIF" conditions.
In my first file, I have the following code :
if(isset($_POST['submit']) {
$selected_radio = $_POST['selection'];
$_SESSION['my_selection'] = $_POST['selection'];
if (($selected_radio == '25') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
elseif (($selected_radio == '50') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
}
That was the easy part.
If either "radio button" is selected, I have a Javascript function which opens a "new (child) window"
That's also easy.
But, then, comes the hard part : within that new window, the user has to select from another choice of radio buttons :
if(isset($_POST['submit']) {
$selected_radio = $_POST['my_response'];
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
}
Basically, this means : if the user selects "yes" in the current (child) window, then the window closes, and the parent window re-directs to "25.php" or "50.php"..........depending on the value of the $_SESSION['my_selection'] --- which was selected earlier in the parent-window
But, for some reason, it's not working. My code is executing only the FIRST IF-condition :
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
It is completely ignoring the second one.............even if the user had earlier selected "50" in the parent-window.
My first thought was : the SESSION value of the radio-button ---- $_SESSION['my_selection'] --- was not being carried-over into the new (child) window.
But, I used "echo" to verify that this was working properly. The value was indeed being carried-over into the new (child) window.
However, after the child-window closes, and the parent-window is re-directed, I used "echo" again to track any errors........and it showed that : the value of $_SESSION['my_selection'] is always equal to "25" !
In a nutshell : why is the second IF-statement being ignored??
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25')
^ ^
A single equals sign is an assignment and as the value you are assigning is truthy the if will always evaluate to true. Use == for a comparison.
You are using = instead of == in nearly all statements:
if (($_POST['my_response'] = 'yes')
should be
if (($_POST['my_response'] == 'yes')
This way, you don't check if $_POST['my_response'] is equal to "yes", but if it is possible to assign "yes" to $_POST['my_response']. As a result, all your if statements are true.

How to add Buy Now button in cs-cart

I'm creating a buy-now button cs-cart add-on. I've created an add-on and I've made the add-to-cart functionality work. But cant redirect it to checkout page. I have used "fn_redirect("checkout")" function for the redirection. And used this function below this:
"fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);"
What should I do for redirection?
Edit:
My controller (buy_now.php)
if ($mode == 'add') {
if (empty($auth['user_id']) && Registry::get('settings.General.allow_anonymous_shopping') != 'allow_shopping')
{
return array(CONTROLLER_STATUS_REDIRECT, "auth.login_form?return_url=" . urlencode($_REQUEST['return_url']));
}
// Add to cart button was pressed for single product on advanced list
if (!empty($dispatch_extra)) {
if (empty($_REQUEST['product_data'][$dispatch_extra]['amount'])) {
$_REQUEST['product_data'][$dispatch_extra]['amount'] = 1;
}
foreach ($_REQUEST['product_data'] as $key => $data) {
if ($key != $dispatch_extra && $key != 'custom_files') {
unset($_REQUEST['product_data'][$key]);
}
}
}
$prev_cart_products = empty($cart['products']) ? array() : $cart['products'];
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
fn_save_cart_content($cart, $auth['user_id']);
$previous_state = md5(serialize($cart['products']));
$cart['change_cart_products'] = true;
fn_calculate_cart_content($cart, $auth, 'S', true, 'F', true);
if (md5(serialize($cart['products'])) != $previous_state && empty($cart['skip_notification'])) {
$product_cnt = 0;
$added_products = array();
if (!empty($added_products)) {
Registry::get('view')->assign('added_products', $added_products);
if (Registry::get('config.tweaks.disable_dhtml') && Registry::get('config.tweaks.redirect_to_cart')) {
Registry::get('view')->assign('continue_url', (!empty($_REQUEST['redirect_url']) && empty($_REQUEST['appearance']['details_page'])) ? $_REQUEST['redirect_url'] : $_SESSION['continue_url']);
}
fn_redirect(Registry::get('config.https_location') . "/checkout");
// $msg = Registry::get('view')->fetch('views/checkout/components/product_notification.tpl');
//fn_set_notification('I', __($product_cnt > 1 ? 'products_added_to_cart' : 'product_added_to_cart'), $msg, 'I');
$cart['recalculate'] = true;
} else {
fn_set_notification('N', __('notice'), __('product_in_cart'));
}
}
unset($cart['skip_notification']);
$_suffix = '.checkout';
}
`
hook template : add_to_cart.post.tpl
{$id = "buy_now_{$product.product_id}"}
<button id="opener_{$id}" name="dispatch[buy_now.add..{$product.product_id}]" class=" vs-button buynow_btn_">Buy Now</button>
I'd consider putting your fn_redirect("checkout") code into an if/else statement to see what's happening with it.
If you have this code:
fn_add_product_to_cart($_REQUEST['product_data'], $cart, $auth);
Then surely you can put the redirection to the checkout page underneath it?
$redirect_url = "http://testcheckout.com?test=test"; //change this!
$errorMessage = "I should have redirected already....";
if (!empty($redirect_url)) {
//if it's not empty then it should redirect.
fn_redirect($redirect_url);
//Then put a die statement in here just to check it's not executing
//anything else and you can see if the redirect has a problem:
die(print_r($errorMessage, true ));
}
else {
//otherwise there is a problem with the supplied redirect url (empty)
die(print_r($redirect_url, true ));
}
I'd also take a look at this page which might help more:
http://forum.cs-cart.com/topic/6873-direct-buy-button/
Hope that helps!

Magento product page in IE8: add to cart increments by default value, ignoring quantity field. form.js line 266 errors

The default form.js file appears to be erroring when "add to cart" is clicked. This prevents the value of the quantity field from correctly being passed. Magento substitutes the default value instead. This script doesn't error and the quantity field works just fine in other browsers.
Furthermore, the add to cart button works correctly when I switch back to the default theme. Any ideas on where I should start. does anything here stand out as not being IE8 friendly? I haven't modified any of the add to cart functions, nor the form.js file.
Update: I have a browserstack account. Debugging shows that VarienForm is undefined which throws two "'productAddToCartForm' is null or not an object" errors. Form.js which defines VarienForm is being loaded in the header so it should be available for the inline JS.
since you can't see line numbers here is the line in question:
this.regionSelectEl.options.add(option);
Here is the function:
update: function()
{
if (this.regions[this.countryEl.value]) {
var i, option, region, def;
def = this.regionSelectEl.getAttribute('defaultValue');
if (this.regionTextEl) {
if (!def) {
def = this.regionTextEl.value.toLowerCase();
}
this.regionTextEl.value = '';
}
this.regionSelectEl.options.length = 1;
for (regionId in this.regions[this.countryEl.value]) {
region = this.regions[this.countryEl.value][regionId];
option = document.createElement('OPTION');
option.value = regionId;
option.text = region.name.stripTags();
option.title = region.name;
if (this.regionSelectEl.options.add) {
this.regionSelectEl.options.add(option); //***this is line 266***
} else {
this.regionSelectEl.appendChild(option);
}
if (regionId==def || (region.name && region.name.toLowerCase()==def) ||
(region.name && region.code.toLowerCase()==def)
) {
this.regionSelectEl.value = regionId;
}
}
if (this.disableAction=='hide') {
if (this.regionTextEl) {
this.regionTextEl.style.display = 'none';
}
this.regionSelectEl.style.display = '';
} else if (this.disableAction=='disable') {
if (this.regionTextEl) {
this.regionTextEl.disabled = true;
}
this.regionSelectEl.disabled = false;
}
this.setMarkDisplay(this.regionSelectEl, true);
} else {
if (this.disableAction=='hide') {
if (this.regionTextEl) {
this.regionTextEl.style.display = '';
}
this.regionSelectEl.style.display = 'none';
Validation.reset(this.regionSelectEl);
} else if (this.disableAction=='disable') {
if (this.regionTextEl) {
this.regionTextEl.disabled = false;
}
this.regionSelectEl.disabled = true;
} else if (this.disableAction=='nullify') {
this.regionSelectEl.options.length = 1;
this.regionSelectEl.value = '';
this.regionSelectEl.selectedIndex = 0;
this.lastCountryId = '';
}
this.setMarkDisplay(this.regionSelectEl, false);
}
Region selection should only be triggered in cart or checkout. Check if your template uses own JS-Files in skin/frontend/your_theme/js. I think your template was made for a different Magento version so the CSS-Selectors in your phtml-files don't fit the JS anymore. So you have either to use Matching JS-Files or compare your product template to the one in base template and adapt CSS classes.

make an ifnot statement and if statement in one line

I'm trying to make an if statement with 2 conditions. One that checks if one variable is NOT present & does NOT matches the word "good2go" and the other that checks to make sure "body" variable is present. I'm trying to trip the error message here. Here is what I have and what I've tried, and none of it seems to work.
if (stripos($_POST['check'], 'good2go') == FALSE && $_POST['body']) {
$error = true; }
if (!$_POST['check'] == 'good2go' && $_POST['body']) {
$error = true; }
if (!stripos($_POST['check'], 'good2go') && $_POST['body']) {
$error = true; }
if ((!stripos($_POST['check'], 'good2go')) && $_POST['body']) {
$error = true; }
How do I get this to work?
here's the entire code of contact_us.php this has the validation code and the email code.
$error = false;
if (isset($_GET['action']) && ($_GET['action'] == 'send')) {
// Winnie the pooh check
//$t = tep_db_prepare_input($_POST['verify']);
if (!isset($_POST['check']) && !$_POST['check']=='good2go' && isset($_POST['body'])) {
$error = true;
} else { // Winnie the pooh Check
$name = tep_db_prepare_input($_POST['name']);
$email_address = tep_db_prepare_input($_POST['email']);
//IP recorder start
$ipaddress = $_SERVER["REMOTE_ADDR"];
$ip = "\n\nIP: " . $ipaddress;
$content = "\n\nName: ".$name."\n\nComments: ".$_POST['enquiry'];
$product = tep_db_prepare_input($_POST['product']);
if ($product) {
$product_text = "\n\nProduct Interest: ".$product; }
$content_ip = $content . $product_text. $ip;
$enquiry = tep_db_prepare_input($content_ip);
//IP recorder end
}
// BOF: Remove blank emails
// if (tep_validate_email($email_address)) {
// tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);
// tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// } else {
// $error = true;
// $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
if (! tep_validate_email($email_address)) {
$error = true;
$messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
}
if ($enquiry == '') {
$error = true;
$messageStack->add('contact', ENTRY_EMAIL_CONTENT_CHECK_ERROR);
}
if ($error == false) {
tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);
tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// EOF: Remove blank emails
}
}
Solution to your updated problem:
if (!isset($_POST['check']) || !$_POST['check']=='good2go' || !isset($_POST['body'])) {
$error = true;
}
The reason for the pipes vs ampersands is that you want to throw an error if ANY of the fields has issue. Also, you want to check if body is NOT set vs IS set. Glad this worked out for you!
and the other that checks to make sure "body" variable is not present.
if(stripos($_POST['check'], "good2go") !== false && !isset($_POST['body'])){
//code here
}
According to PHP docs regarding the stripos function:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
So you need to change the first line to:
// Doing stripos checks you MUST use === (not ==)
if (stripos($_POST['check'], 'good2go') !== FALSE && $_POST['body']) {
$error = true; }
And to check if there is no $_POST['body'] you can change the above to:
if (stripos($_POST['check'], 'good2go') !== FALSE && (!isset($_POST['body'])) {
-- Update --
According to your comment, you need $_POST['check'] to equal 'good2go', then you shouldn't be using stripos as it will check for the existence of good2go regardless if it's exactly equal, or part of a string; 'wow this hamburger is good2go'.
So I would change the conditional to:
if (((isset($_POST['body'])) && (strlen($_POST['body']) > 0)) && ((!isset($_POST['check'])) || ($_POST['check'] !== 'good2go'))) {
// Post body has a value and Post check DOES NOT equal good2go, someone is hax0rin!
}
You may want to read up on Cross-site request forgery as it seems right inline with what you are working on.
One that checks if one variable is present & matches the word "good2go"
isset($_POST['check']) AND $_POST['check'] == 'good2go'
and the other that checks to make sure "body" variable is not present.
!isset($_POST['body'])
so, just put them together
if (isset($_POST['check']) AND $_POST['check'] == 'good2go' AND !isset($_POST['body'])) {
$error = true;
}
try this:
if(!empty($_POST['check']) && $_POST['check']=='good2go' && empty($_POST['body'])) { $error=true; }
Consider using empty instead of isset if your $_POST['body'] can be present with an empty value.
No need for all those unneeded functions. What you are trying to achieve is:
if (isset($_POST['check']) && $_POST['check']=='good2go' && !isset($_POST['body']) {
// your code
}
However, As per the title of the question: Use a ternary statement. Syntax is as such
$var = <condition> ? <true> : <false>;

modify function and If statement

I have a inputbox that displays a default text "Input Network ID Here...". I need to take $inputUri and check it against function checkUrl($string) to see if its still there. If the text hasn't been cleared then display the addError message
public function checkUrl($string)
{
$inputUri = 'Input Network ID Here...';
if(empty($string) || preg_match("#^([A-Z0-9][A-Z0-9_ -]*(?:.[A-Z0-9][A-Z0-9_ -]*)+):?(d+)?/?#i", $string))
{
return true;
}
else
{
if( isset($this) )
{
$this->addError("Input Network ID");
}
return false;
}
}
I think the problem is that the code is never entering the else block.
Perhaps you should change your first if line (line 3) to this:
if( (empty($string) || preg_match("#^([A-Z0-9][A-Z0-9_ -]*(?:.[A-Z0-9][A-Z0-9_ -]*)+):?(d+)?/?#i", $string) && $string != $inputUri )

Categories