March 8, 200818 yr Hello Folks... Im just wondering can I pass a JS value into a PHP string? <form> <script>DateInput('orderdate', true, 'DD-MON-YYYY')</script> <input type="button" onClick="alert(this.form.orderdate.value)" value="Show date value passed"> </form> This is the value I want: this.form.orderdate.value any help would be great. Thanks
March 8, 200818 yr You can only pass variables from JS to PHP by refreshing the page, as PHP is client-side. If you submit a form PHP can easily pick up the values from it for example with the following form: <form action="form.php" method="POST"> <input type="text" name="myinput"> <input type="submit" name="mysubmit"> </form> You can use PHP to check the form has been submitted and collect the value from the input with: if(isset($_POST['mysubmit'])){ $myvar = $_POST['myinput']; } Change $_POST to $_GET if you are using the GET method. There's some info on ways to get JS values to PHP here: http://forums.devshed.com/javascript-devel...-how-16698.html Hope this helps - Eris
March 8, 200818 yr Go get XAJAX. PHP and JS start talking interactively. The important distinctions to make: 1. PHP is parsed and written SERVER SIDE 2. JS is parsed and used CLIENT SIDE XAJAX bridges the gap (as does AJAX), and is a great framework.
March 8, 200818 yr XAJAX might be overkill in this situation simply AJAX will do, if you don't need a reply from the server (other than a "yep, got it") take a look at jQuery's functions
March 8, 200818 yr Whoa hold your horses guys... Js is client side.. so ONCLICK should run before the submit event occurs ( eg the form vars are collected and posted back to the server - PHP ). What you're trying to do should work but you may need to use javascript onclick to A: kill the submit event B: make you form modifications C: recreate the submit event - data gets sent to server Even getting into ajax is a little over the top for this EDIT: Made this, not 100% sure if it's what you were meaning but I hope it is <?php if($_POST) { echo '<div style="border: 1px solid #ccc; margin: 10px 0;">'; echo '<p>Your form was submited, here are the results</p>'; echo '<pre>'; print_r($_POST); echo '</pre>'; echo '</div>'; } ?> <script> function mydatefunction(inf) { //this function will SET my special date or whatever to the input value inf.value = 'my special date'; } </script> <form name="myform" method="POST" action="" > <input id="orderdate" type="text" name="orderdate" /> <input id="test" type="button" onClick="mydatefunction(this.form.orderdate)" value="Test with a button" /> <input id="test2" type="submit" onClick="mydatefunction(this.form.orderdate)" value="Test with a submit" /> </form>
March 9, 200818 yr Author Thanks Guys and Gals! Warren thanks for your form, Ive made a few changes: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <script type="text/javascript" src="calendarDateInput.js"> /*********************************************** * Jason's Date Input Calendar- By Jason Moon http://calendar.moonscript.com/dateinput.cfm * Script featured on and available at http://www.dynamicdrive.com * Keep this notice intact for use. ***********************************************/ </script> </head> <body> <?php if($_POST) { echo '<div style="border: 1px solid #ccc; margin: 10px 0;">'; echo '<p>Your form was submited, here are the results</p>'; echo '<pre>'; print_r($_POST); echo '</pre>'; echo '</div>'; } ?> <script> function mydatefunction(inf) { //this function will SET my special date or whatever to the input value inf.value = 'orderdate'; } </script> <form name="myform" method="POST" action="<?php $_SERVER['PHP_SELF'] ?>" > <script>DateInput('orderdate', true, 'DD-MON-YYYY')</script> <input id="orderdate" type="submit" onClick="mydatefunction(this.form.orderdate)" value="submit" /> </form> </body> </html> for some reason when I change the date the values on screen change but when submitted goes back to the old values? http://www.ben-smith.net/cal/cal.php
March 9, 200818 yr The main problem here ( I think ) is that changes to the date fields ( via the calender or manual ) are not being reflected in the hidden field. Change the hidden field to text and you will see what I mean. So the values aren't going back to the old values.. the values are never being changed in the first place. Ok after some playing round I realised what was going on. The script inserts the elements into the form, doh!@ Here is the ACTUAL ouputed code, after the javascript has ran <body> <script> </script> <form action="" method="post" name="myform"> <input id="orderdate" type="text" name="orderdate" value="10-MAR-2008"/> <table cellspacing="2" cellpadding="0"> <tbody> <tr> <td valign="middle"> <select id="orderdate_Month_ID" class="calendarDateInput" onchange="orderdate_Object.changeMonth(this)"> </select> </td> <td valign="middle"> <select id="orderdate_Day_ID" class="calendarDateInput" onchange="orderdate_Object.changeDay(this)"> </select> </td> <td valign="middle"> <input id="orderdate_Year_ID" class="calendarDateInput" type="text" onblur="orderdate_Object.fixYear(this)" onkeyup="orderdate_Object.checkYear(this)" onkeypress="return YearDigitsOnly(window.event)" value="2008" title="Year" maxlength="4" size="4"/> </td> <td valign="middle"> <a id="orderdate_ID_Link" onmouseout="return orderdate_Object.iconHover(false)" onmouseover="return orderdate_Object.iconHover(true)" href="java script:orderdate_Object.show()"> </a> <span id="orderdate_ID" onmouseout="orderdate_Object.handleTimer(false)" onmouseover="orderdate_Object.handleTimer(true)" style="border: 1px solid dimgray; position: absolute; visibility: hidden; width: 126px; background-color: white;"> </span> </td> </tr> </tbody> </table> <input id="orderdate" type="button" value="submit" onclick="mydatefunction(this.form.orderdate****)"/> </form> </body> I've highlighted what I believe is the problem. Your script creates a bunch of new elements, mainly those for modifying the date. But it also creates a hidden input to store the changes in. Ironically this hidden input gets called orderdate ( as per your script function call ) and all the other elements get named orderdate_somethinghere eg orderdate_day So when you change the date to 18th of the month the hidden input then changes to 18-MAR-08 etc. The nice thing about this script I noticed is that it appears to destroy the JavaScript elements on submit, meaning you only get your date posted. So basically you have 2 inputs, one created via the JavaScript and your submit button both id'd as 'orderdate' When your JavaScript decides to update the hidden input, it basically says "Oh ****, which one?" Rename your submit to something like id="orderdate_Submit" and you should be all swell. EDIT: Also, remove the JavaScript function ( and call ) that I wrote in my example. It will over write any date changes when you click submit. Good luck
March 10, 200818 yr Author Thanks thats great! Im not to hot on JS! Thanks again... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <script type="text/javascript" src="calendarDateInput.js"> /*********************************************** * Jason's Date Input Calendar- By Jason Moon http://calendar.moonscript.com/dateinput.cfm * Script featured on and available at http://www.dynamicdrive.com * Keep this notice intact for use. ***********************************************/ </script> </head> <body> <?php if($_POST) { echo '<div style="border: 1px solid #ccc; margin: 10px 0;">'; echo '<p>Your form was submited, here are the results</p>'; echo '<pre>'; print_r($_POST); echo '</pre>'; echo '</div>'; } ?> <form name="myform" method="POST" action="<?php $_SERVER['PHP_SELF'] ?>" > <script>DateInput('orderdate', true, 'DD-MON-YYYY')</script> <input id="orderdate_Submit" type="submit" onClick="mydatefunction(this.form.orderdate)" value="submit" /> </form> </body> </html>
Create an account or sign in to comment