mortalc Posted July 1, 2010 Share Posted July 1, 2010 Can a variable assigned in PHP be used later in the code? E.g. <?php $x = 0 ?><script type="text/javascript">if ($x==1){...}</script> Link to comment Share on other sites More sharing options...
GermanPrince Posted July 1, 2010 Share Posted July 1, 2010 Yep, but you can't access a variable declared in PHP in JavaScript. Link to comment Share on other sites More sharing options...
End User Posted July 1, 2010 Share Posted July 1, 2010 Yep, but you can't access a variable declared in PHP in JavaScript.Sure you can. print <<<EOM<script type="text/javascript">var foo = $my_php_variable;</script>EOM; Link to comment Share on other sites More sharing options...
Synook Posted July 1, 2010 Share Posted July 1, 2010 You need an additional PHP block in the code, e.g. <?php $x = 0 ?><script type="text/javascript"><?phpif ($x==1){...}?></script> And I suppose GermanPrince meant, without re-declaring it with JS code. Link to comment Share on other sites More sharing options...
GermanPrince Posted July 1, 2010 Share Posted July 1, 2010 print <<<EOM<script type="text/javascript">var foo = $my_php_variable;</script>EOM;That code makes no sense what-so-ever.-GermanPrince Link to comment Share on other sites More sharing options...
GermanPrince Posted July 1, 2010 Share Posted July 1, 2010 You need an additional PHP block in the code, e.g.<?php $x = 0 ?><script type="text/javascript"><?phpif ($x==1){...}?></script> And I suppose GermanPrince meant, without re-declaring it with JS code. Now that makes sense.-GermanPrince Link to comment Share on other sites More sharing options...
mortalc Posted July 1, 2010 Author Share Posted July 1, 2010 But if the code to be executed can only be done in javascript (document.getElementById("FORMID").submit()), what can I do? Link to comment Share on other sites More sharing options...
Synook Posted July 1, 2010 Share Posted July 1, 2010 Then let it submit, and handle it on the action page. That's what I'm saying - try not to think about combining PHP and JavaScript. They serve different purposes, and it may be better to just focus on the server-side action at the moment. Link to comment Share on other sites More sharing options...
mortalc Posted July 1, 2010 Author Share Posted July 1, 2010 Maybe if I lay out what I'm trying to do:1. Check if 2 POST variables are undefined2. If they are, submit the form (or in another way go to a different webpage).As far as I can tell, 1. is only possible in PHP, and 2. is only possible in Javascript. Link to comment Share on other sites More sharing options...
thescientist Posted July 1, 2010 Share Posted July 1, 2010 Maybe if I lay out what I'm trying to do:1. Check if 2 POST variables are undefined2. If they are, submit the form (or in another way go to a different webpage).As far as I can tell, 1. is only possible in PHP, and 2. is only possible in Javascript.before the variables in 1 become $_POST variables, you can use javascript functions to check form elements either as a person moves through the form elements, or before they submit. It's actually very common (and good practice) for form elements to have their values checked client side first before being submitted and dealt with on the server-side. Link to comment Share on other sites More sharing options...
mortalc Posted July 2, 2010 Author Share Posted July 2, 2010 This wouldn't work, because the POST variables are being passed to the wepage from another.Example:Form is @ www.mysite.comSubmitted form goes to www.mysite.com/welcome.phpI want to stop people going straight to www.mysite.com/welcome.phpThe form would send them back to www.mysite.com. Link to comment Share on other sites More sharing options...
thescientist Posted July 2, 2010 Share Posted July 2, 2010 This wouldn't work, because the POST variables are being passed to the wepage from another.Example:Form is @ www.mysite.comSubmitted form goes to www.mysite.com/welcome.phpI want to stop people going straight to www.mysite.com/welcome.phpThe form would send them back to www.mysite.com.why can't you use a header? you can just submit to a script, it doesn't have to be a "page" Link to comment Share on other sites More sharing options...
Synook Posted July 3, 2010 Share Posted July 3, 2010 Let the form submit to welcome.php, validate the input, and if it's bad, just redirect back to the index page. Link to comment Share on other sites More sharing options...
mortalc Posted July 3, 2010 Author Share Posted July 3, 2010 wait...Can I put header() in an if function?So: <?php if !(isset($_POST["name"]) header(location:www.mysite.com); ?> Link to comment Share on other sites More sharing options...
boen_robot Posted July 3, 2010 Share Posted July 3, 2010 That would be <?php if (!isset($_POST["name"]) header('Location: http://www.mysite.com'); ?> but indeed - "Yes, we can!".The only thing to keep in mind is that there must be nothing before the "<?php". And I mean it! Not even a whitespace!In general, you can't stop people from going directly to the action script. If they want to bypass the form, they can. The only thing you can do is ensure is that they have (in one way or another) given you correct data. Link to comment Share on other sites More sharing options...
End User Posted July 3, 2010 Share Posted July 3, 2010 That code makes no sense what-so-ever.Actually, it makes perfect sense. It's printing out a snippet of javascript, and the "$my_php_variable" is a PHP var. If you used 'foo' later in the page in a javascript call, it would contain the value of "$my_php_variable" was when PHP printed it out. If "$my_php_variable" contained "John", then alert(foo); would pop a dialog box that said "John". I'm sorry if this seemed puzzling to you, but I use this sort of thing all the time, and I assure you that it does work. For example, $my_php_variable is equal to "GermanPrince": print <<<EOM<script type="text/javascript">var foo = $my_php_variable;</script>EOM;Later in the page there is this javascript code:<script type="text/javascript">alert('Welcome, ' + foo + '!');</script>This would pop a dialog box that said, "Welcome, GermanPrince!" Link to comment Share on other sites More sharing options...
thescientist Posted July 3, 2010 Share Posted July 3, 2010 wait...Can I put header() in an if function?So:<?php if !(isset($_POST["name"]) header(location:www.mysite.com); ?> Yup. I think we may have covered this in another thread, or maybe it was someone else....Anyway, in the page that holds the form, you can create a script that checks the form variables to see if the submit button has been set, and if it hasn't then load the page. if it has been, the check for the variables of the form for validation, and the redirect. so your form redirects to itself, upon load, it does the form checker script, and that's how you can get a redirect out of page that also has HTML.edit: here it is.http://w3schools.invisionzone.com/index.php?showtopic=32070 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.