...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
May 29, 2013

mod_rewrite, Adding a Condition

A A’s Question:

I have this .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ zone.php [L]

Right now it works pretty well. How can I make this RewriteRule not work in a certain directory?

I do not want the rule to work for anything in domain.com/manage/

Change your code with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule (?!^manage/)^.*$ /zone.php [L,NC]

Add a condition by using the RewriteCond keyword to apply the rule if the directory is not manage

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/manage(/|$)
RewriteRule ^(.*)$ zone.php [L]
Read more

How to get value of radio button with dynamically generated name

Daniel Young’s Question:

I have a whole bunch of radio buttons with dynamically generated names like below:

        <input type="radio" id="Red<?php echo $wineID; ?>" name="category<?php echo $wineID; ?>" value="Red" <?php if ($category == "Red") { ?>checked="true"<?php } ?>>
          <label for="Red<?php echo $wineID; ?>">Red</label>

        <input type="radio" id="White<?php echo $wineID; ?>" name="category<?php echo $wineID; ?>" value="White" <?php if ($category == "White") { ?>checked="true"<?php } ?>>
          <label for="White<?php echo $wineID; ?>">White</label>

        <input type="radio" id="Sparkling<?php echo $wineID; ?>" name="category<?php echo $wineID; ?>" value="Sparkling" <?php if ($category == "Sparkling") { ?>checked="true"<?php } ?>>
          <label for="Sparkling<?php echo $wineID; ?>">Sparkling</label>

I need to get the selected value and add it into my dataString for an ajax call to update my database. How can this be done using jQuery?

You can use attribute selector to get the element

$('input[name="category<?php echo $wineID; ?>:selected"')

However this uses a PHP inline script, so it will only work if rendered at the page load.

Or easiest would be:

console.log($(":radio:selected").val());
Read more

Post does not work

Hawiak’s Question:

I have a really annoying problem.

I have a form and when I Submit it it doesn’t set the post.

<form action="pages/post-reply" method="post">
    <div class="row">
        <div class="large-12 columns">
            <textarea name="comment" placeholder="Comment on admin"></textarea>
        </div>
    </div>
    <input type="submit" name="submit" class="tiny button" value="Post"/>
</form>

I use a framework called Processwire and Foundation but I don’t think this has anything to do with it.

When I try it out on my webserver (a dedicated host) it works. I am using a WAMP install on Windows 8. Could this have anything to do with it?

When I use:

    echo $_SERVER['REQUEST_METHOD'];

It just says:

GET

Edit: .htaccess file:
http://textdump.net/raw/2212/

This may be caused by redirection either from the page of pages/post-reply itself or .htaccess rule on the root directory who is redirecting the request pages/post-reply as a GET Request.

Check your .htaccess rule

Read more

Jquery Select Box not working on dynamically generated elements

Yaar Mallang Jeha’s Question:

I am with a problem. I am using jQuery.SelectBox for the select box and dropdowns.

It is working fine when the elements are loaded with the page load. But its not working when they are loaded by the ajax i.e on dynamicaly generated elements it is not working.

You can check the file here :- http://rvtechnologies.info/brad/jquery.selectBox.js

CSS codes are style declaration and stylesheets, once the element gets added to the DOM they will be loaded or applied.

Check the name of id, classes and attributes of the generated elements using tools like firebug and see the generated markup.

Read more
May 28, 2013

Date of user local-system not hosted server – PHP

X10nD’s Question:

When I add this to the code

$today1 = date("d");

it gives me the server date and not the user local system date.

I do not want to use date_default_timezone_set()

How can I get the date of the local user(not web host server) at that point of time using PHP.

PHP runs on the server so using functions like time() and localtime() will not get you the time of the client.

Javascript runs on client’s system thus it can get the time of the client. But how to make the time available to a PHP script is a tricky part.

The answer is AJAX Request, you can send the time from ajax to you script file which will use that value and give you results.

Like

var clientTime = Date.now();
$.get("yourpage.php", { time: clientTime }, function(data) 
   // the response in data
});
Read more

Prevent double submit

User2307958’s Question:

I have a submit button:

<div class="submitForm">
<input type="submit" id="submit" value="SAVE" class="greyishBtn" onclick ="if(!DW_CheckIfImagesInBuffer())
{return confirm('No images');
}else{
 btnUpload_onclick(2)}"/>  </div>

I’m tryng to to prevent submit form twice disabling button in this way:

else{
 btnUpload_onclick(2) this.disabled=true; this.value='Please Wait...';}

Button when submit change to Please Wait, but form is not submitted.

Any help?

TKS ALL

The problem is ; in the else statement

btnUpload_onclick(2) this.disabled=true; this.value='Please Wait...';
               //   ^ Here             ^ Like this

Change to

btnUpload_onclick(2); this.disabled=true; this.value='Please Wait...';
Read more
May 27, 2013

Can't detect changed id

Xegano’s Question:

When I change the id of a button I cannot find the new id with on.(“click”). The function console.log() does detect that it’s changed but I cannot detect it with the on() function.

HTML:

<form id="formName" action="" method="post">
    <input type="submit" id="submitBtn" value="Submit" />
</form>
<button id="change">Change</button>

JS:

$("#change").on("click", function(){
    $("#submitBtn").attr("id", "NewBtn"); 
});

$("#formName").submit(function(e){
    e.preventDefault();
});

$("#NewBtn").on("click", function(){
    alert("Hello"); 
});

So I need it to alert “Hello” after I have clicked on change. It does change the id I checked that with inspect element.

Fiddle: http://jsfiddle.net/WvbXX/

.attr() function does not have a callback and thus it cannot be checked unless you setup an interval using setInterval but the function itself executes pretty soon so you are not going to need it.

For solving the problem in hand event delegation proposed by tymeJV is the right way to do it.

Read more

jQuery animate not working at all

Nordvind’s Question:

I want to set a background-color div property on DOM ready, then perform some animation, like this: $("#test").css('background-color','red').animate({ 'background-color' : '#FFF'}, 500);. Div looks like this <div id="test">11</div>, and it has a background-color property set in CSS file #test{
background-color:#FFF;
}

Now the question – why animation is not working? You can see the code here – http://jsfiddle.net/hzdcM/

You need to have jQuery UI included to make you background-color animation to work

Demo --> http://jsfiddle.net/hzdcM/1/

.animate() function does not support animating colors. Use jQuery color plugin. or you can use jQuery UI Library.

UI Library is very heavy for just the color use.

Also you have use backgroundColor not background-color to animate the color properly.

$("#test")
   .css('backgroundColor','red')
   .animate({ 'backgroundColor' : '#FFF'}, 500);
Read more

Why Shall i use a PHP Framework

Khan Koder’s Question:

I m New to WEB Development I use Apache2.0i and NOTEPAD++ for web development i came to hear about some new words CAKEPHP, CodeINgniter Zend Etc.

Can any one please Tell me in detail why do i need Frameworks what is the benefit of these frameworks i dont really understand these frameworks.

You would just never find a good example of raw PHP code over the whole internet.
So, learning raw PHP you will learn ancient techniques and bad practices as well.

While framework tutorials will teach you a better ways, good practices and safer code.

Despite this question is very unwelcomed in this community. I would like to point you http://www.phpframeworks.com/

And please focus on core PHP if you are starting on PHP Development, it will make you much stronger.

Read more

Error: Unknown column 'Date' in 'field list'

Manju’s Question:

I am working on a blood bank data base but i am repeatedly getting a Error: Unknown column ‘Date’ in ‘field list’.there is no spelling error in either php or mysql so here is my php code

<?php
    $con=mysqli_connect("localhost","root","","bloodbank");
    // Check connection
   if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

   $sql="INSERT INTO users       (First,Last,Date,Email,Contact,Bloodgroup,Gender,address,City,username,password)
    VALUES
    ('$_POST[first]','$_POST[last]','$_POST[dob]','$_POST[email]','$_POST[contact]','$_POST[bg]',
'$_POST[sex]','$_POST[address]','$_POST[city]','$_POST[username]','$_POST[password]')";

    if (!mysqli_query($con,$sql))
    {
    die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";

    mysqli_close($con);
    ?></code>

Basic explanation would be that Your query does not matches with your table structure

INSERT INTO users(First,Last,Date
                             ^ // Right here you are using it

Check your table structure using tools like phpMyAdmin and if the field does exist, Date is a reserved word so that might be creating the problem.

Read more
...

Please fill the form - I will response as fast as I can!