php header issue wont redirrect on duplicate function - header

I have two nearly identical php functions both are functioning but for some reason the header will only run on the first one. I have no idea why this could be happening. I've seen some similarly titled post but none have solved or are the same as the problem I have so please don't remove this question. I've tried removing the closing php tag no help I've tried just about everything you could imagine but no cigar. the code is definitely running as echo's work at the end of the code so does exit(); but for some reason header just doesn't work. Any help would be greatly appreciated I will try anything. Also the reason I need this to work is so that when the page is refreshed it doesn't resubmit the form. I know this isn't much to go on so feel free to ask me questions.
function setComments($conn){
if (isset($_POST['commentsubmit'])) {
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = mysqli_real_escape_string($conn, $_POST['message']);
$sql = "INSERT INTO comments (uid, date, message) values('$uid','$date', '$message')";
$result = mysqli_query($conn,$sql);
header("Location: videos.php");
}
}
duplicate code with alterations.
function setComments2($conn){
if (isset($_POST['commentsubmit2'])) {
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = mysqli_real_escape_string($conn, $_POST['message']);
$sql = "INSERT INTO comments2 (uid, date, message) values('$uid','$date','$message')";
$result = mysqli_query($conn,$sql);
header("Location: home.html");
}
}

The header function calls were located AFTER other code.
PHP.net says this:
"Remember that header() must be called BEFORE any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file."

Related

hookHeader in Prestashop, breaks flow whatever I do

I want to add a custom js file my_sdk.js in my Prestashop module.
I register the hook header and prepare the function,
but whatever I do inside, breaks something.
My my_sdk.js file is correctly added by addJS() and it works.
The problem is that, after that, the cart is no more updated.
Whatever I write inside the hookHeader, it's executed but then the popup showing after the 'add to cart' action, stops working. The popup doesn't show up and the total amount of the cart is not updated until I refresh the page. It's not a problem of my js file, even if I don't import it and write something else (like an echo), I have the same problem.
It works only if I don't write anything (or comment everything) inside function hookHeader().
public function install() {
return (bool) $this->registerHook(['header']);
}
public function hookHeader($params) {
$this->context->controller->addJS(_PS_MODULE_DIR_ . $this->name . '/views/js/my_sdk.js'); // this line breaks the popup
echo "hello, this breaks the popup"; // also just this line breaks the popup
}
I really don't understand why. Tried also with hookDisplayHeader, same thing. The website works, without errors, but the cart is not updated and the popup doesn't show.

A letter is displayed after body tag in Prestashop website

I have a prestashop website http://www.myparaweb.com/ which displays a 'l' letter after body tag, it's returned also in the ajax json response after back office loging so that the login is never succeed.
I tried to view the theme header.tpl, and the admin header.tpl and i didn't found it there
Attached the code screenshot and the json return with the 'l' in the beginning.
I'd like to remove this 'l' letter.
Thanks
Edit: As i begin to found where the 'l' letter occurs, i debugged the __constructor function in the Dispatcher class
protected function __construct()
{ die("test");
$this->use_routes = (bool)Configuration::get('PS_REWRITING_SETTINGS');
...
}
This returned the 'l' letter like this
<html><head></head><body>ltest</body></html>
Edit2: i tried to debug into the getInstance function like this
public static function getInstance()
{ die('test');
if (!self::$instance) {
self::$instance = new Dispatcher();
}
return self::$instance;
}
I got
<html><head></head><body>ltest</body></html>
When looking at the source code the l character appears before your <!DOCTYPE HTML> declaration.
l<!DOCTYPE HTML>
<html lang="fr-fr">
<head>
<meta charset="utf-8" />
<title>Default Ciblo theme</title>
As it appears on every page we are sure it's not a Controller but it might be the /classes/controller/FrontController.php classe.
If I were you, I would debug by printing die("test") at different steps in Prestashop process to find where this character comes from. By starting with the FrontController :
class FrontControllerCore extends Controller
{
public function __construct()
{
die("test");
[...]
}
}
Then try with the init() function... Then initContent() and finally displayHeader.
This might be a long process taking you 1 hour or more of debug but you'll be sure to find the exact place of this bug.
EDIT: From your bellow comment, we can tell that this bug happens before the controller is called.
We will then start to debug from the /index.php:
<?php
/**
[...]
*/
die("test");
require(dirname(__FILE__).'/config/config.inc.php');
Then:
<?php
/**
[...]
*/
require(dirname(__FILE__).'/config/config.inc.php');
die("test");
Dispatcher::getInstance()->dispatch();
With this you will know if the bug happens before the dispatcher of after. If it happens in the config file, repeat the process in this file for each line to find which include causes this problem.
EDIT: From your comment bellow, the problem happens after the Dispatcher call so you'll have to dig into /classes/Dispatcher.php. Start by debuging the __cunstructor() function and then dispatch() function.
Please try searching through all the framework files you edited. Its probably in one of them.
An insane way to find out is to search for " 1 " in all php files. I used notepad++ to open about all .php files in folders i edited to search for "1". Since the propability of having a "1" in you code is limited, you can easily find it. Go to each subfolder , search for ".php" and open all files in notepad++, search for "1" in all open documents. Saves time rather than going through all code.

Phpbb new successful member registration pop up window

I would like to create a pop up welcome message for the new member that's successfully registered.
But I m having problem of finding where should I put the code, I have check the ucp_register.html ,, but I don't think that is the display content after the member successfully registered, can anyone help me please? Thanks
It would likely to be a more robust solution to display the popup on the first time the user is logged in as an activated user -- after registration they may not be activated, or they may close the browser window immediately after registration.
The way to do this would be to add a column (say, user_JBL_seen_message INT to the phpbb_users table in the database, then modify functions.php to check that column:
In functions.php, find:
// The following assigns all _common_ variables that may be used at any point in a template.
Before, add:
if($user->data['is_registered'] && $user->data['is_active'] && !$user->data['is_bot'])
{
if(isset($user->data['user_JBL_seen_message']) && !$user->data['user_JBL_seen_message']))
{
$showPopup = true;
$sql = 'UPDATE ' . USERS_TABLE . ' SET user_JBL_seen_message = 1
WHERE user_id = ' . (int)$user->data['user_id'];
if (!$result = $db->sql_query($sql))
{
return false;
}
}
}
Then, find:
$template->assign_vars(array(
After, add:
'JBL_POPUP' => $showPopup,
Then, you can add the popup HTML code to your overall_header.html template file, where appropriate...
<!-- IF JBL_POPUP -->
.... your HTML popup code here.....
<!-- END IF -->
If you don't want existing users to see the popup, then fill the new column with 1s.
I also agree with Damien's suggestion to use a jQuery UI dialog rather than a popup -- most users' browsers will block popups. However, use jQuery in noconflict mode to avoid conflicts with other mods.

drupal multiple node forms on one page

I have a view that displays several nodes. I want to place node form below each displayed node. Both node_add and drupal_get_form directly in template.php works fine, but I get forms with same form ID of NODETYPE_node_form and validation and submitting does not work as expected.
If you had to put several node forms on one page, what would be your general approach?
Progress so far...
in template.php while preprocessing node
$author_profile and $content is set before.
$unique = $vars['node']->nid;
$node = new StdClass();
$node->uid = $vars['user']->uid;
$node->name = $vars['user']->name;
$node->type = 'review';
$node->language = '';
$node->title = t('Review of ') . $vars['node']->realname . t(' by ') . $vars['user']->realname . t(' on ') . $content->title;
$node->field_review_to_A[0]['nid'] = $nodeA->nid;
$node->field_review_to_B[0]['nid'] = $vars['node']->nid;
$node->field_review_to_profile[0]['nid'] = $author_profile->nid;
if(!function_exists("node_object_prepare")) {
include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
}
//$vars['A_review_form'] = drupal_get_form('review_node_form', $node);
$vars['A_review_form'] = mymodule_view($node, $unique);
in mymodule module
function mymodule_view($node, $unique) {
if(!function_exists("node_object_prepare")) {
include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
}
$output = drupal_get_form('review_node_form_' . $unique, $node);
return $output;
}
function mymodule_forms($form_id, $args) {
$forms = array();
if (strpos($form_id, "review_node_form_") === 0) {
$forms[$form_id] = array('callback' => 'node_form');
}
return $forms;
}
function mymodule_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) && $form_id != $form['type']['#value'] .'_node_form' && $form['type']['#value'] == 'review') {
$type = content_types($form['#node']->type);
if (!empty($type['fields'])) {
module_load_include('inc', 'content', 'includes/content.node_form');
$form = array_merge($form, content_form($form, $form_state));
}
$form['#pre_render'][] = 'content_alter_extra_weights';
$form['#content_extra_fields'] = $type['extra'];
//$form['#id'] = $form_id;
//$form['#validate'][0] = $form_id . '_validate';
$form['title']['#type'] = 'value';
$form['field_review_to_A']['#type'] = 'value';
$form['field_review_to_B']['#type'] = 'value';
$form['field_review_to_profile']['#type'] = 'value';
}
}
Questions
My take on summarizing unclear questions...
Is this good general approach for displaying multiple node forms on same page?
Is it OK to copy/paste code from content modules content_form_alter function in function mymodule_form_alter? Will it not brake things if content module is updated?
Should i set $form['#id']? Without it all forms has same HTML form ID of node_form, with it ID is unique, like review_node_form_254. Thing is that there is no difference of how form is submitted. Setting $form['#validate'][0] does not seem to influence things too. Maybe I should set $form[button]['#submit'][0] to something else? Now its node_form_submit.
Why validation does not work even with unique form id and form validate function? If i submit last form without required field all previous forms gets particular fields red. should I make my own validation function? what level - form or field? Any tips on where to start?
You need to implement hook_forms() to map different ids to the same builder function.
The NODETYPE_node_form ids you mention are already an example of this mechanism, as they are all mapped to the same builder function (node_form()) within the node modules node_forms() implementation.
You can find links to more examples in the 'Parameters' explanation off the drupal_get_form() function.
This was exceptionally useful to me.
Documentation on all the drupal APIs is so lacking - I was tearing my hair out. The crucial bit for me, that I don't think is covered anywhere else on the net:
CCK adds its fields to your form through hook_form_alter() . But it does this based on the form_id. So if the form ID is different (cause you want multiple ones on the same page), you need to replicate a bit of the CCK code to add the fields to your form regardless.
That is what this does brilliantly.
I really did not dig to the bottom of it, but it seems to me that you pretty much did all the relevant digging by yourself.
From what I understand by inspecting the code only, you are right in thinking that content_form_alter() is the show-stopper.
Maybe this is a naïve suggestion, but what about writing your own implementation of hook_form_alter() starting from the content_form_alter() code but changing the conditions that make the alteration to occur? I am thinking to something along these lines:
function modulename_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node']) &&
stripos($form_id, $form['type']['#value'] .'_node_form')) {
...
}
}
I did not actually test the code above, but I hope it is self-explenatory: you actually trigger the changes for a give customised type of content if MYCCKTYPE_node_form is a substring of the actual form_id (es: MYCCKTYPE_node_form_234).
Hope this helps at least a bit... Good luck! :)
EDIT: TWO MORE THINGS
It just occurred to me that since your custom implementation of hook_form_alter() will live aside the CCK one, you would like to add a check to avoid the form to be modified twice something like:
&& $form_id != $form['type']['#value'] .'_node_form'
The other way you might try to implement this by not using CCK but implementing you custom type programmatically (this might even be an advantage if you plan to use your module on various different sites).

problem with RegisterClientScriptBlock

i have to run following javascript through one of my method. But its not running
Whats wrong with the code.
private void fillGrid1()
{
GridView1.DataSource = myDocCenter.GetDsWaitingForMe(Session["UserID"].ToString());
HiddenField1.Value = { myDocCenter.GetDsWaitingForMe(Session["UserID"].ToString()).Tables[0].Rows.Count).ToString();
GridView1.DataBind();
String csname1 = "PopupScript1";
String csname2 = "ButtonClickScript1";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
{
StringBuilder cstext2 = new StringBuilder();
cstext2.Append("<script type=\"text/javascript\"> ");
// You can add JavaScript by using "cstext2.Append()".
cstext2.Append("var count = document.getElementById('ctl00_ContentPlaceHolder1_HiddenField2');");
cstext2.Append("var count = '100';");
cstext2.Append("document.getElementById('sp2').innerHTML = count;");
cstext2.Append("script>");
cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
}
}
Your script tag is not properly closed.
Change
cstext2.Append("script>");
to
cstext2.Append("</script>");
On top of what adamantium said, your JS looks a bit strange. You seem to declare and set the count variable twice - did you mean to do this.
Following that, best thing to do, render the page then view source. is your JS getting rendered to the page? try and stick an alert in there... is it firing?
> cstext2.Append("var count =
> document.getElementById('ctl00_ContentPlaceHolder1_HiddenField2');");
I would use the ClientID property here. HiddenField2.ClientID
RegisterClientScriptBlock emits the script just after the <form> tag openning. Browser executes this script just after the tag openning as well but referenced elements are not processed yet at this time - browser cannot find them.
RegisterStartupScript method emits the script just before the <form> tag ending. Nearly all page elements are processed by the browser at this place and getElementById could find something.
See http://jakub-linhart.blogspot.com/2012/03/script-registration-labyrinth-in-aspnet.html for more details.