Jump to content

how to use/what to do with return $value; in JS


helloise

Recommended Posts

have this in my javascript code but have no idea on how a returned value works/where does it return to???? what do i do/or can i do with a value that gets returned???i am asking this so i can learn on how to use it cos i am new to all this and dont know how it works...please show/explain to me please?thankshere my full code:PHP Code:<?php use_stylesheets_for_form($form) ?> <?php use_javascripts_for_form($form) ?> <html> <head> <script type="text/JavaScript"> function refreshPage(s) { window.location.reload(); if((s.options[s.selectedIndex].value) = "zed-catcher") { var $index = s.selectedIndex; var $value = s.options[$index].value; alert (s.selectedIndex); alert ($value); window.location.reload(); return &value; //where does it return to ???? how/what to do with a return value??? } } </script> </head> </html> <body> <form action="<?php echo url_for('adminservice/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" '?>> <?php if (!$form->getObject()->isNew()): ?> <input type="hidden" name="sf_method" value="put" /> <?php endif; ?> <table > <tfoot> <tr> <td colspan="2"> <?php echo $form->renderHiddenFields(false) ?>  <a href="<?php echo url_for('adminservice/index') ?>">Back to list</a> <?php if (!$form->getObject()->isNew()): ?>  <?php echo link_to('Delete', 'adminservice/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?> <?php endif; ?> <input type="submit" value="Save" /> </td> </tr> </tfoot> <tbody> <?php echo $form->renderGlobalErrors() ?> <tr> <th><?php echo $form['name']->renderLabel() ?></th> <td> <?php echo $form['name']->renderError() ?> <?php echo $form['name']?> </td> </tr> <tr> <th><?php echo $form['logo_url']->renderLabel() ?></th> <td> <?php echo $form['logo_url']->renderError() ?> <?php echo $form['logo_url'] ?> </td> </tr> <tr> <th><?php echo $form['call_center_number']->renderLabel() ?></th> <td> <?php echo $form['call_center_number']->renderError() ?> <?php echo $form['call_center_number'] ?> </td> </tr> <tr> <th><?php echo $form['catcher_id']->renderLabel(); $catcher_id = $form->getObject()->getCatcherId(); $catcher = LpmCatcherPeer::getByCatcherId($catcher_id); $catcher_name = $catcher->getName(); ?></th> <td> <?php echo $form['catcher_id']->renderError() ?> <select name="services" onchange="refreshPage(this.form.services)"> <?php $catcher_names = LpmCatcherPeer::getByAllNames(); foreach($catcher_names as $row) { ?> <option value="<?php echo $row->getName()."/".$row->getId();?>" <?php if($row->getName() == $catcher_name) echo ' selected="selected"'; ?> ><?php echo $row->getName();?></option> <?php } if ($row->getName() == "zed-catcher") //just thought with the return in my java script i can pass the newley selected value back to here and if it meets the if i echo these lines?? { echo $form['service_code']->renderLabel(); echo $form['service_code']->renderError(); echo $form['service_code']; } ?> </select> </td> </tr> <tr> <th><?php echo $form['price_description']->renderLabel() ?></th> <td> <?php echo $form['price_description']->renderError() ?> <?php echo $form['price_description'] ?> </td> </tr> </tbody> </table> </form> </body>

Link to comment
Share on other sites

Does this work at all? It doesn't look like it should... it's a really improper mix of PHP and JavaScript.If it does, it's one weird mix of PHP with JavaScript, I'm guessing done by a framework of some sort. You'll have to consult that framework for details on how do they work.

Link to comment
Share on other sites

Does this work at all? It doesn't look like it should... it's a really improper mix of PHP and JavaScript.If it does, it's one weird mix of PHP with JavaScript, I'm guessing done by a framework of some sort. You'll have to consult that framework for details on how do they work.
im not mixing anything i have my call the the javascript line: <select name="services" onchange="refreshPage(this.form.services)">then i have my java script:<script type="text/JavaScript"> function refreshPage(s) { if((s.options[s.selectedIndex].value) = "zed-catcher") { var $index = s.selectedIndex; var $value = s.options[$index].value; alert (s.selectedIndex); alert ($value); return $value; } } </script>i just simply want to know where i have the return $value, how must i use it further??? so after my <select name="services" onchange="refreshPage(this.form.services)">how can i access $value in php??thanks
Link to comment
Share on other sites

Ah... that's more like it... you had "&value" in your first post, and that actually almost means something in some languages (I'm thinking C, but you're close to having a syntactically valid PHP).First things first, you currently have

if((s.options[s.selectedIndex].value) = "zed-catcher")

If your intention is to check if the selected option value is "zed-catcher", you need to use

if((s.options[s.selectedIndex].value) == "zed-catcher")

instead. The way you have it now, the selected option's value (regardless of what it is) will be changed to "zed-catcher", and your code will resume as if the condition is true, because the result of "=" is the newly assigned value, which in this case is "zed-catcher" which evaluates to true.As for the actual question about the return value... the returned value, in pretty much all programming languages (including PHP and JavaScript) is returned to the expression which called it.When browsers execute event handlers in attributes, they evaluate the expression in the attributes. If it evaluates to true, they resume calling any events that are about to follow and execute their default actions. The best example of that is the onclick event of a submit button - the default browser action after the event is to first call the onsubmit event and then submit the form. If the onclick returned true or returned nothing, that's what will happen. If it returned false, the browser will not start the onsubmit event and will not submit the form.The situation with other events is a little trickier though, because browsers execute some events in a different order, so you may end up executing some events in some browsers, and other events in other browsers.The above is all in JavaScript context though... you can't access $value in PHP. It's still a JavaScript variable. The fact that it starts with "$" doesn't make it a PHP variable.

Link to comment
Share on other sites

  • 2 weeks later...

also, you asked how return's work. It's pretty simple. Typically returns are used in functions, to, well, return a value to the line making the function/method call. it's pretty simple, let me show you a basic example.

<script type="text/javascript">function messager(name){  var text = "";  if(name == "paul"){	var text = "Hello " + name + ".  How are you today?";  }else{	var text = "Who are you?";  };  return text;};var userName = 'paul';var alertMessage = messager(userName);alert(alertMessage);</script>

if userName is paul, then you should get a customized message. If not, then you get a different message.http://www.w3schools.com/js/js_functions.asp

Link to comment
Share on other sites

also, you asked how return's work. It's pretty simple. Typically returns are used in functions, to, well, return a value to the line making the function/method call. it's pretty simple, let me show you a basic example.
<script type="text/javascript">function messager(name){  var text = "";  if(name == "paul"){	var text = "Hello " + name + ".  How are you today?";  }else{	var text = "Who are you?";  };  return text;};var userName = 'paul';var alertMessage = messager(userName);alert(alertMessage);</script>

if userName is paul, then you should get a customized message. If not, then you get a different message.http://www.w3schools.com/js/js_functions.asp

thank you very much what i actually want ed to know is how you would call the function in php????thus:
    $message = messager(name);

i tried the above idea but i get error saying function is undefined :)thank you

Link to comment
Share on other sites

you are trying to use javascript within php, or visa versa. you can only use/execute php within php tags, you can echo a javascript code as string into a page which will executed as normal javascript code, when called upon.Options, using previous example from ScientistOption 1 : run javascript as echo string value within PHP tags

<?php$message = '<script type="text/javascript">messager("paul")</script>';echo $message;?>

Option 2 : run javascript as echo string value with user variable using PHP variable, within PHP tags

 <?php$user="paul";$message = '<script type="text/javascript">messager("'.$user.'")</script>';echo $message;?>

Option 3 : run javascript as normal, But! with user variable using PHP variable, within PHP tags

 <?php$user="paul";?><script type="text/javascript">messager("<?php echo $user; ?>")</script>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...