Jump to content

alert in a php while loop SOLVED with thanks in the final post


niche
 Share

Recommended Posts

Is there a way to produce an alert while a php while loop is running? If so, what's the keyword(s) do I need to reference? I'd like to pause my script as my page is being created.

Edited by niche
  • Like 1
Link to comment
Share on other sites

Guest So Called

I'm no expert on this and from your post count you're more expert than I... But it seems to me that your alert is JavaScript, and that JS and PHP are not synchronous (won't communicate with each other) unless you use AJAX techniques. You can certainly pause a PHP script but it will require a reply from the client to continue. IOW: AJAX.

Link to comment
Share on other sites

I'm following you, but how do I get the php variable into the JS var when the JS is rendered in php? Script so far:

<?php$str = "Hello World";echo '<script type="text/javascript">';echo 'var str = ?????';echo 'alert(str)';echo '</script>';?>

Edited by niche
Link to comment
Share on other sites

Guest So Called

You can always get variables from PHP to JS. The problem is getting the JS response back to the server side script. Focus on that. IMO you need AJAX, or at least I can't imagine any browser-to-server interaction that doesn't use that.

Link to comment
Share on other sites

Thanks,but I don't need anything back in this case. I just need to display what's in a php var assigned to a JS var when the JS var is rendered in php. This displays nicely, but there's no var in it:

<?phpecho '<script type="text/javascript">';echo 'alert("Hello World")';echo '</script>';?>

how do I re-define a php var to a JS var when the JS var is being rendered in php?

<?php$str = "Hello World";echo '<script type="text/javascript">';echo 'var str = ?????';echo 'alert(str)';echo '</script>';?>

Edited by niche
Link to comment
Share on other sites

Guest So Called

Okay, then that's probably simpler:

<?php$str = "Hello World";echo '<script type="text/javascript">';echo 'var str = ?????';echo 'alert(' . $str . ')';echo '</script>';?>

Link to comment
Share on other sites

Guest So Called

Niche, I didn't even try it, but it seems so obvious to me I can't imagine why it wouldn't work. How is that different from this?

<?phpecho '<script type="text/javascript">';echo 'alert('Hello World')';echo '</script>';?>

I'll run it live on my server tomorrow if you haven't fixed it by then. (BTW I should have deleted the line with ???? in it.) Remember to approach coding problems with baby steps. Get a vastly more simple version working then add to it step by step. If things don't work, cut the code to the essentials.

  • Like 1
Link to comment
Share on other sites

This didn't display anything for me but an error in firebug:

<?php$str = "Hello World";echo '<script type="text/javascript">';echo 'alert(' . $str . ')';echo '</script>';?>

But when I added double quotes to the alert like so:

$str = "Hello World";echo '<script type="text/javascript">';echo 'alert(" '. $str . ' ")';echo '</script>';

.. it worked. This won't work according to my results:

<?phpecho '<script type="text/javascript">';echo 'alert('Hello World')';echo '</script>';?>

But when putting Hello World in double quotes instead, it will. Doing it with single quotes works like this:

$str = "Hello World";echo "<script type=\"text/javascript\">";echo "alert('hello world' )"; // if removing the single quotes from hello world, it won't work. JS needs some kind of quotes here when using alert to alert a stringecho "</script>";

I guess it has to do with how PHP renders things in single or double quotes.

  • Like 1
Link to comment
Share on other sites

(1)<?php$str = "Hello World";echo '<script type="text/javascript">';echo 'alert(' . $str . ')';echo '</script>';?>outputs alert(Hello World) because 'hello world' is not within any quotes it thinks they a variable references to what????? error (2)$str = "Hello World";echo '<script type="text/javascript">';echo 'alert(" '. $str . ' ")';echo '</script>';outputs alert("Hello World") OK! this is text string I can do that! nooo problem <?php echo '<script type="text/javascript">';echo 'alert('Hello World')';echo '</script>';?> outputs same as 1 with same error$str = "Hello World";echo "<script type=\"text/javascript\">";echo "alert('hello world' )"; // if removing the single quotes from hello world, it won't work. JS needs some kind of quotes here when using alert to alert a stringecho "</script>"; last two example mentioned, are just a swap around of using single or double quotes in php which will output alert("Hello World") or alert('Hello World'), both using quotes which means they will be read as text string and not as variable reference. You have to distinguish what is text string in php, and what will be outputted as a text string for JavaScript in this situation, without any quotes(single or double) to define it as a string in JavaScript, produces error.

  • Like 1
Link to comment
Share on other sites

This is one for my topic scrapbook. This one is truly a keeper! I've been needing something like this for years. Before recently, I didn't understand javascript well enough to even ask this question. A BIG thanks to justsomeguy who framed the answer, and to So Called, who knew he was pointed in the right direction, and to Don E, who solved my problem, and special thanks to dsonesuk who, as usual, provides that "something extra" in his posts. Thanks again to you all. This was a real present to wake up to.

Link to comment
Share on other sites

Guest So Called

It was late last night, and I make simple coding mistakes when I'm tired. The missing quotes would have come out after a simple debug test, evident when the box didn't work correctly and upon viewing source code.

Link to comment
Share on other sites

Me too. Like I said, you knew you were going in the right direction. You gave me confidence that there was an answer and you knew what it was. That's part of what these forums are about. Every post won't be be a homerun especially after a long day. I will always appreciate your help. Thanks again So Called.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...