PHP if / else statements don't seem to work for contact us with email - statements

I've created some if / else statements to get a download when a user hit click
but dont work ,because you can direct download without field name or email ,please help me
contact. php for my site
http://my-easy-woodworking-projects.com
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'lulu#yahoo.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script>
var str = "download";
document.write(str.link("http://www.myshedplans.com/12BY8SHED.pdf"));
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>

I'm not quite sure why that if/else isn't working for you but one thing that can happen is the interpreter forgets about the if statement when it gets down to the else. A way to get around this is by echoing your html code within the conditional block like bellow:
<?php
if ($mail_status) {
echo "<script>";
echo "var str = \"download\";";
echo "document.write(str.link(\"http://www.myshedplans.com/12BY8SHED.pdf\"));";
echo "</script>";
}
else {
echo "<script language=\"javascript\" type=\"text/javascript\">";
echo "alert('Message failed. Please, send an email to gordon#template-help.com');";
echo "window.location = 'contact_page.html';";
echo "</script>";
}
?>

Related

Error while uploading images with EasyMDE

I am using EasyMDE text editor on my Notemaking website, but for some reason, the uploadImage functionality doesn't work.
This is how I instantiate EasyMDE.
var easyMDE = new EasyMDE({
autofocus: true,
autorefresh: true,
spellChecker: false,
minHeight: '180px',
uploadImage:true,
});
Can some one help me fix this issue or point me in the right direction of how to solve it?
I was searching for a PHP method but this is what I got :
https://github.com/Ionaru/easy-markdown-editor/pull/71
Edit:-
I figured it out in PHP
You need to also set the "imageUploadEndpoint" value to another file and save your image and print json.
This is how I did in PHP.
Client-Side:
var easyMDE = new EasyMDE({
uploadImage:true,
imageUploadEndpoint: 'upload.php'
});
Server-Side(in upload.php):
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
header('Content-Type: application/json');
//logic for saving the file
if(image_saving_successful)
{
$url = the_filepath_for_the_image_saved
echo '{"data": {"filePath": "'.$url.'"}}';
http_response_code(200);
exit;
}else{
echo '{"error": "your_error_message"}';
http_response_code(400);
exit;
}
}else{
header('location: index.php');
exit;
}
?>
There are different response code for different error messages. Check Readme.md

My cookie code is not working

I'm trying to use cookies, but my most basic code below doesn't work. The page stays blank and is not saying anything. My cookies are enabled, and I'm using UTF-8 encoding.
I tried codes from various websites. They are working fine until I make even a little change (e.g. use another name for the cookie or put another text in the cookie). Can someone please tell me what I'm doing wrong?
<?php
$username = "Test";
setcookie("username", $username, time()+365*24*60*60, '/', "localhost");
echo $_COOKIE["username"];
?>
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
The superglobal $_COOKIE is not updated by the set_cookie() function. If you want your cookie to be immediately available you need to also manually set the value of $_COOKIE[$name]. Something like this
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
$_COOKIE[$cookie_name] = $cookie_value;
?>

Perl Regex on a mechanize->content page

I am fiddling around in perl and I managed to retrieve a HTML page from a source. However I just want to retrieve 1 particulair line. The line starts with a date formatted as follow: dd/mm/YYYY.
The HTML is in displayed with print $resp->content(); $resp being a response from a $mechanice->submit_form();
This is where the resp is made:
my $resp = $m->submit_form(
//bunch of data
},
);
How do I achieve this? I am familiar with PHP but I just started with Perl.
Thanks
Here's an example from some Mechanize code that I have.
my $mech = WWW::Mechanize->new();
$mech->get("url that takes you to the page with the form");
$mech->submit_form(form_name => 'someform',
fields => {'user_name' => 'user's
'password' => 'password'},
button => 'submit');
return if not $mech->success();
my $content = $mech->content();
if ($content =~ m|(\d{2,2}/\d{2,2}/\d{4,4}.*)|g) {
print "My line: $1\n";
}

opencart 1.5 how to add module in a header

Please some one tell me how can I position modules like slider or banner in header or how can we define additional regions for the modules.
Well, I am working on it.
Firstly, you should add a new position in the admin page.
Which means you should change three files and add some lines into each.
For example, if you want to add slideshow into header, you should add a new position in
$this->data['text_header'] = $this->language->get('text_header'); // in admin/controller/slideshow.php
////////////////////////////////////////////////
$_['text_header'] = 'Header'; // in admin/language/slideshow.php
////////////////////////////////////////////////
<?php if ($module['position'] == 'header') { ?> // in admin/view/slideshow.tpl
<option value="header" selected="selected"><?php echo $text_header; ?></option>
<?php } else { ?>
<option value="header"><?php echo $text_header; ?></option>
<?php } ?>
Then a new position for slideshow has been defined. and you can choose it in the admin page.
After that, you should add those codes in catalog/view/header.tpl
<?php foreach ($modules as $module) { ?>
<?php echo $module; ?>
<?php } ?>
Then, in the catalog/controller/header.php, add some codes like content_top.php does:
$layout_id = 1;
$module_data = array();
$this->load->model('setting/extension');
$extensions = $this->model_setting_extension->getExtensions('module');
foreach ($extensions as $extension) {
$modules = $this->config->get($extension['code'] . '_module');
if ($modules) {
foreach ($modules as $module) {
if ($module['layout_id'] == $layout_id && $module['position'] == 'header' && $module['status']) {
$module_data[] = array(
'code' => $extension['code'],
'setting' => $module,
'sort_order' => $module['sort_order']
);
}
}
}
}
$sort_order = array();
foreach ($module_data as $key => $value) {
$sort_order[$key] = $value['sort_order'];
}
array_multisort($sort_order, SORT_ASC, $module_data);
$this->data['modules'] = array();
foreach ($module_data as $module) {
$module = $this->getChild('module/' . $module['code'], $module['setting']);
if ($module) {
$this->data['modules'][] = $module;
}
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) {
$this->template = $this->config->get('config_template') . '/template/common/header.tpl';
} else {
$this->template = 'default/template/common/header.tpl';
}
$this->render();
Then all done.
The only problem for this code is the slideshow cannot display as a slideshow but a static picture or pictures.
Wish you could solve it out and reply to me.
My question post:
Opencart developement: change slideshow's position
do what Huawei Chen wrote and then add these to header:
<script type="text/javascript" src="catalog/view/javascript/jquery/nivo-slider/jquery.nivo.slider.pack.js"></script>
<link rel="stylesheet" type="text/css" href="catalog/view/theme/default/stylesheet/slideshow.css" media="screen" />
and slider will be working
You can learn from controller/common/column_right.php
Check module position (line 50), you can adapt it to "header":
$module['position'] == 'column_right'
Set the output (line 69), you can adapt it to something like "header_mod" :
$this->data['modules']
You need to modificate module at admin to show additional module position.
There is no "header" module position options at admin page.
You can also use an extension that places 2 positions in header and footer. It create the same hooks, like column_left and content_top, etc.
it works with VQmod for opencart 1.5x
I think this extension will make your life super simple. http://www.opencart.com/index.php?route=extension/extension/info&token=extension_id=14467
You can add unlimited number of positions to your header, add columns and change there width all through admin
Regards
This guide helped me:
http://www.mywork.com.au/blog/create-new-module-layout-position-opencart-1-5/
I was using 1.5.6.1 Opencart version.
I read all of your answer. Forget all things.
It's very easy to call any module into header section.
Open your catalog/controller/common/header.php file
$this->data['YOUR_MODULE_NAME'] = $this->getChild('module/YOUR_MODULE_NAME');
Now Open .tpl file of header, which is located at
catalog/view/theme/YOUR_THEME/template/common/header.tpl
and echo your desire module whenever you want, like as:
<?php echo $YOUR_MODULE_NAME;?>
That's it. you are done.
You must know that your new position is a new child.
file: catalog/controller/common/header.php
$this->children = array(
'module/language',
'module/currency',
'module/cart'
);
add your new position, like this:
$this->children = array(
'common/content_new',
'module/language',
'module/currency',
'module/cart'
);
now you can echo your new position in your header.tpl

Regexp to find youtube url, strip off parameters and return clean video url?

imagine this url:
http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
what is the cleanest and best regexp to do the following:
1.) i want to strip off every thing after the video URL. so that only http://www.youtube.com/watch?v=6n8PGnc_cV4 remains.
2.) i want to convert this url into http://www.youtube.com/v/6n8PGnc_cV4
Since i'm not much of a regexp-ert i need your help:
$content = preg_replace('http://.*?\?v=[^&]*', '', $content);
return $content;
edit: check this out! I want to create a really simple WordPress plugin that just recognizes every normal youtube URL in my $content and replaces it with the embed code:
<?php
function videoplayer($content) {
$embedcode = '<object class="video" width="308" height="100"><embed src="' . . '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="308" height="100" wmode="opaque"></embed></object>';
//filter normal youtube url like http://www.youtube.com/watch?v=6n8PGnc_cV4&feature=rec-LGOUT-real_rn-2r-13-HM
//convert it to http://www.youtube.com/v/6n8PGnc_cV4
//use embedcode and pass along the new youtube url
$content = preg_replace('', '', $content);
//return embedcode
return $content;
}
add_filter('the_content', 'videoplayer');
?>
I use this search criteria in my script:
/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/
You could just split it on the first ampersand.
$content = explode('&', $content);
$content = $content[0];
Edit: Simplest regexp: /http:\/\/www\.youtube\.com\/watch\?v=.*/
Youtube links are all the same. To get the video id from them, first you slice off the extra parameters from the end and then slice off everything but the last 11 characters. See it in action:
$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$url = $url.left(42); // "http://www.youtube.com/watch?v=1rnfE4eo1bY"
$url = $url.right(11); // "1rnfE4eo1bY"
$result = "http://www.youtube.com/v/" + $url; // "http://www.youtube.com/v/1rnfE4eo1bY"
You can uniformize all your youtube links (by removing useless parameters) with a Greasemonkey script: http://userscripts.org/scripts/show/86758. Greasemonkey scripts are natively supported as addons in Google Chrome.
And as a bonus, here is a one (okay, actually two) liner:
$url = "http://www.youtube.com/watch?v=1rnfE4eo1bY&feature=...";
$result = "http://www.youtube.com/v/" + $url.left(42).right(11);
--3ICE
$url = "http://www.youtube.com/v/6n8PGnc_cV4";
$start = strpos($url,"v=");
echo 'http://www.youtube.com/v/'.substr($url,$start+2);