Thursday, April 28, 2011

AJAX checkbox if statement

I've got a bunch of variables being pulled from form ID's before being sent in a query string to a PHP. However, as one input is a checkbox I'm trying to get AJAX to set the variables value according to whether it's checked or not. i.e..

 if (document.getElementById('bold').checked) { var bold = "true"; 
 }
 else { var bold = "false"; }

But, it doesn't actually send anything to the PHP. I'm guessing syntax a lot of the time, so if anyone can correct I'd be most grateful.

From stackoverflow
  • As long as you've given it a name it should be accessible as "on" or "off" in the backend e.g.

    var $checked = $_POST["checkboxname"] == "on";
    

    So your javascript is redundant.

    WiseDonkey : Ideally, this would the answer but it's being sent in a GET and the string literally just has bold=true or bold= and nothing there. I tried using isset() or empty()... neither would work. I think it needs to be sorted in the JS.
    Rob Stevenson-Leggett : Can't you just do var $isBold = $_GET["bold"] == "true" then?
  • Firstly, instead of: var bold = "true";

    I think you're actually looking for something like: document.getElementById('bold').value = "true";

    Secondly, if the checkbox is not checked nothing will be sent to the PHP script (unchecked checkboxes are not sent as part of a form submission).

    WiseDonkey : Didn't work on that as I have to still declare the var afterwards. Secondly, normally that is the case but this is submitted by an ajaxFunction onChange of anything. So the value does seem to be submitted whatever the cirumstances.
    Narcissus : If that's the case then I agree with chosta... we need to see the sending code.
    DaNieL : Agree, if the checkbox isnt checked, nothing will be send with the form
    WiseDonkey : Thanks very much, I've actually identified the 'ahem' problem... PHP side the variable was typo'd out. The proposed method I used in the question has worked. :/ Sorry to faff you around guys, thanks for the effort.

What is the best way to create a "fully" modular ZF-based application?

For the past time I have been working on a Zend Framework based webshop application that we are building from scratch. The application has been successfully been set up for several clients, but we are stumbling upon more and more problems regarding extensibility and modularity of the application.

A lot of clients require a lot of different approaches on some matters, often requiring customizing default modules or extending them. I have been looking on the web for Zend Framework based code that could guide me to this and so far Magento has been the only example I have come up with. Although Magento seems to do a very good job offering extensibility and modularity, the code is far too complex and extensive for my current needs (or so it seems).

In an ideal situation, it would work as follows:

  • Each client gets the "base" application installed
  • Modules, controllers, models should be easily overridden or extended
  • New modules should be easily created

Are there any other Zend Framework based applications or other pointers that could guide me in the right direction?

Thanks in advance.

From stackoverflow
  • Zend Framework is a... framework. What you're looking for is a full blown system. That's your job as a developer is to build a system on top of a framework (whether Zend or your own).

    People usually tell you not to reinvent the wheel, but in this case you need to develop your own system for your company to easily be able to develop and deploy for new clients.

    Like already stated, it is a very complex answer you are looking for. You will not get that answer.

    Aron Rotteveel : Thanks for the answer, I appreciate it. I do want to make clear however that I am not looking for a "copy / paste" solution that I can use. I am very interested in the solutions others have possibly thought of encountering this problems, since I haven't thought of one so far.
    Matt : Others don't know the full requirements of what you really need. Sure you listed basic requirements, but that's way too vague. Anything more complicated and you're basically asking for a consultation, which people normally get paid really good money for.
  • I think version 1.8 of the Zend Framework is going to address a lot of the issues with building modular applications. Especially with the new (Zend_Application) it should allow for auto loading of module resources, which isn't easy to do at this point.

    I don't know much about it, but this may be something to look into. 1.8 should be out by the end of this month.

RDBMS Key Confusion

I'm trying to sort out the database behind my company's website.

We deal with both Sites and Residents. At the moment the Site Reference is included in the Residents table as a foreign key but, obviously, this is only in 1NF.

Site references are of the form nnnnn (12345, for example). Each resident has their own reference within the Site of the form nnn (starting at 001 and ending at a maximal 999).

The problem is, when I take it to 2NF:

Site(Reference, Name, Charge)

Residency(SiteReference, ResidentReference,)

Resident(Reference, Address, Username, Password)

The Resident Reference will never uniquely identify one Resident as, if there are 2 sites, then there will be 2 001s. I can't use username as this field is only populated once (and if!) they have registered for our web service. I'd simply introduce a new autonumber ID for Residents but there's something niggling at me that there is a more elegant solution. Can anyone suggest what it is?

From stackoverflow
  • You need to "free" the resident reference from the site - e.g. make it a ResidentID and make sure each ResidentID is globally unique within the Resident table. Every Site must also have a globally unique SiteID within the Site table.

    Then and only then will you be able to create a good db schema.

    The Residency table will provide the link between residents (identified uniquely by their ResidentID) and the sites (again - uniquely identified by their SiteID).

    Marc

    Bailz : That's what I was thinking. Thanks for the confirmation.

What's the easiest way to encrypt a file in c#?

Beforehand :

I have read indeed the other topics on SO, but I can't find an answer in them.
(The others are about config-files, or a list of techniques)

My question thus is very simple, though a bit subjective (I'll label it beforehand :-)) what is the easiest way..

tx!

From stackoverflow
  • Data Protection API in C#

  • File.Encrypt is pretty simple - one call (with one parameter).

    Of course, it really depends on what you want the encryption for. File.Encrypt encrypts to the current account, which isn't much use if you're passing the file around. But, given your spec - i.e. easiest way to encrypt a file - it has to be a candidate!

  • Don't believe you have any security just because you encrypt a config file. If someone has access to the encrypted config file, and your executable, containing the password, it's likely to be possible to decrypt your configfile. It's just a little harder.

    And say your config file contains passwords to database connections, it might be possible to get those passwords looking at the network packets.

    Peter : I voted you up, because the downvote may have to do with the fact that you misread my question, and that could have been partly my fault,so I rephrased it.
    Lucas McCoy : I agree with you because it's ok to be 'paranoid' about your security if it matters. If you can encrypt it, it can be decrypted. It just like your answer it makes it a little harder.
  • Encryption is trivial with modern libraries: the hard part is securing the key(s).

    So you need to look at what you're trying to secure, and what threats you are trying to secure against.

    To encrypt a file so only the current user can see it on a client workstation, File.Encrypt is a good choice, or DPAPI with the CurrentUser scope.

    For a configuration file on a single server, DPAPI using the LocalMachine scope is a good choice. You then need to make sure only authorized users are able to log in to the server. Here you're essentially delegating key management to Windows.

    For a configuration file on a server farm, you need to share the key between the servers. RsaProtectedConfigurationProvide is a good choice, but you have more work ensuring that all servers have access to the same key, and that it is protected against unauthorized access (e.g. using a DACL).

  • I recommend the Cryptography Application block in Enterprise Library. Very easy, very flexible.

    MichaelGG : I doubt "easiest" and "Enterprise Library" ever belong together...
    JP Alioto : Really? I find the opposite, but I guess I am just used to using it. Encryption done right by it's very nature is not "easy", but I suppse you're right in that it's part of the question. But, for completeness sake. :)

"50289 Can't perform operation since the project is protected" but why?

I am supplying the correct password

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("H:\M\X\C.xls", 0, , , "password")

'any of these lines cause the error mentioned

Set vbcomp = objWorkbook.VBProject.VBComponents(modname)
objWorkbook.VBProject.VBComponents.Remove vbcomp
objWorkbook.VBProject.VBComponents.Import modpath & modtest

Any ideas what the problem might be? Tools-Macro-Security is set to allow VB project access

From stackoverflow
  • The VBA project itself is protected by a password which is different then the worksheet password. Open the VBA IDE - select Tools-VBA Project Properties and look at the Protection Tab.

    Not sure how or if you can remove the VBA Project password using code.

    adolf garlic : Correct. It does not appear to be possible to removed the password on the VBA project, unless you use a hideous 'sendkeys' type workaround.
  • This may sound extreme, however I managed to get around this by downloading Windows Installer Clean up Utility (look for msicuu2.exe in Google) and removed office 2003. I then reinstalled office 2003 and my problem had disappeared. Hope it solves your probelm too. Alex

PHP: Best Method for Sequential Output?

What is the best approach for setting up sequential output for a PHP installer script? Say I have a page, and want to display incremental output within a DIV provided by a PHP script.

So page.html might contain this:

<div id="output">
    <!-- Script Output Here -->
</div>

And install.php might look like this (for testing purposes):

<?php
// Turn off Output Buffering
ob_end_flush();

// Begin Test Output (5 second intervals)
echo 'Testing incremental output...' . "\n";
sleep(5);
echo 'Step 1...' . "\n";
sleep(5);
echo 'Step 2...' . "\n";
sleep(5);
echo 'Complete!' . "\n";

I was thinking AJAX might be the best approach, but I'm not sure how to approach that. If I load "install.php" via JavaScript, I won't get everything back until the script finishes execution (correct?) - making this entire goal moot. I've seen this done before, but can't find a concrete example of how it was done.

Any help appreciated!

From stackoverflow
  • I doubt Ajax will do what you want - instead you may have to call the ajax fetches more frequently.

    For flushing information out as soon as it's produced you need regular flush()'s after outputting information.

    I've also added into a relevant .htaccess file a line to avoid any buffering due to GZipping the output as well (apache2).

    SetEnvIf Request_URI "/url/dont/buffer/me.php" gzip=0
    

    In the relevant PHP file:

    ini_set('output_buffering', false);
    ini_set('implicit_flush', 'true');
    

    and the appropriate calls to flush(). That pretty much covers all the bases.

  • I would output everything to a text file and updating the DIV via AJAX by just calling the .txt file

    Tony k : Pretty hairy; multiple users? Then you've got multiple text files. Name the text file based on a session? What about multiple tabs? Oh crap I just divided by zero.
    Calvin : Ah, screw it. Just time your install script and create an animated GIF. It's all the same to the user.

How can I set a css ":hover" on a DOM created element in JavaScript?

I'm using the DOM to manage a JSON response from an AJAX function I'm running. The script I'm writing needs to be completely portable, so I am defining the styles for the created elements on the fly (meaning, no linking to an external CSS, and no providing CSS in the HTML doc itself, because I won't have control of the doc).

I'd like to create a hover effect on some of the elements.

example:

 #myDiv:hover { background:#000000; }

Is there a way to define that in the DOM? Or do I have to use mouseover?

From stackoverflow
  • You can dynamically create and manipulate stylesheets. See here for some of the cross-browser issues with this approach.

    I've got a wrapper function lying around which works around some of them; using it, the code would read

    document.createStyleSheet().addRule('#myDiv:hover', 'background:#000000;');
    
  • you may create element with predefined class:

    .h:hover{color: #c00}
    
    var elem = document.createElement('div');
    elem.className = 'h'
    
    johnnietheblack : good, and yes...however i didn't have the ability to link to a stylesheet