3

I need help for using window.location= in echo with PHP. Here is my code:

echo
    '<div class="adsa">
         <div class="adimg125" style="'.$stylea.'">
            <div onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation(); window.location='.$url.'" class="check" style="'.$check.'">Check Monitors</div>
         </div>
    </div>';

My data showing fine but link not working mean its not show in ' ' for opening link. Here is my data which is displayed

<div class="check" onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation(); window.location=/index.php?key=perfectdeposit.biz" style="background: #767a81 none repeat scroll 0 0; color: white; cursor: pointer; display: none; font-size: 13px; padding: 2px 0 3px; position: relative; text-align: center; top: -132px; width: 117px; margin-left: 38px">Check Monitors</div>

You can see here is not show ' ' in that line

window.location=/index.php?key=perfectdeposit.biz

it's need to show like this

window.location='/index.php?key=perfectdeposit.biz'
2
  • You are missing quote after window.location, it should be like window.location="'.$url.'" class="check" Commented May 21, 2017 at 13:15
  • yeah you are right but when i use link you then its showing same as i post above Commented May 21, 2017 at 13:22

2 Answers 2

2

Looks like you need to escape those characters in PHP. Maybe something like this?

echo'<div class="adsa"><div class="adimg125" style="'.$stylea.'">
            <div onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation(); window.location=\''.$url.'\'" class="check" style="'.$check.'">Check Monitors</div>
            </div></div>';

Notice the \ near the window.location.

Here is the output:

$stylea = 'something';
$url = 'http://google.com';
$check = 'test';

<div class="adsa"><div class="adimg125" style="something">
        <div onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation(); window.location='http://google.com'" class="check" style="test">Check Monitors</div>
        </div></div>
Sign up to request clarification or add additional context in comments.

4 Comments

@JerryStone Good deal! Want to mark it as the answer so others will know if they stumble upon the same question? ;)
am new and don't know how can mark. PLEASE do it if you can
@JerryStone Only the person asking the question can mark the answer. You should see a checkmark near the answer - just click that to mark the answer. Look here: d2ppvlu71ri8gs.cloudfront.net/items/2r2Q0s422w3C2x0p1M1z/…
i have do it Thanks
0

I normally use heredoc to avoid quote errors, i.e.:

echo <<< EOF
<div class="adsa"><div class="adimg125" style="$stylea">
    <div onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation(); window.location='$url' class="check" style="$check">Check Monitors</div>
</div>
EOF;

Output:

<div class="adsa"><div class="adimg125" style="some_style">
    <div onclick="event.cancelBubble=true;if (event.stopPropagation) event.stopPropagation(); window.location='/index.php?key=perfectdeposit.biz' class="check" style="something">Check Monitors</div>
</div>

PHP Demo

2 Comments

its not working when i use this then my browser show me blank page error
add a blank line after EOF; . Also add error_reporting(E_ALL); ini_set('display_errors', 1); at the top of your php script, what does it return?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.