...

View Full Version : using javascript in PHP



Jason
03-06-2003, 11:06 PM
I am trying to get a value out of my form. I have a php value to store it in but to reference the value I need javascript. I tried it a few ways but it didn't work. Any ideas?

<?php $newName=?> oForm.elements[e].name;

that is the basic jist but I can't figure out how.


Jason

mordred
03-06-2003, 11:20 PM
:confused:

More code please, your request is mildly confusing. What exactly are you trying to do on the server (PHP) and what on the client (JavaScript)? Mixing of these to "script" locations rarely produces the desired output. When PHP has finished processing the file, all output is send to the client and only then may JavaScript do anything, but is incapable of directly invoking PHP functions.

Jason
03-06-2003, 11:24 PM
In my form I used a php to dynamically create a number of check boxes.

echo "<INPUT TYPE=\"checkbox\" NAME=\"<?php echo $display_time; ?>\" VALUE=\"y\" "
. (($ver_ans == 1)?"CHECKED":"UNCHECKED") . "> " . "$display_time";

what I am trying to do is go through after the user makes their selections in the check boxes and input the names of those into a PostgreSql DB for later use. Since I used the form I was trying to access the name using javascript 'cause I know it can, but I can't get the value into php. Does that make more sence?

Jason

mordred
03-06-2003, 11:30 PM
I'm still confused. Why don't you submit the form after the user has made his choice (that means, the user submits it of course)? The receiving PHP script can distinguish which checkbox value was selected/checked.

Jason
03-06-2003, 11:41 PM
that sounds nice and simple except the part where I get confused. What do you mean by recieveing PHP script? This is all in a popup window. And I still don't get how to figure out which boxes are checked or not. Thanks for all the replies.


Jason

mordred
03-06-2003, 11:59 PM
Well, if you have checkboxes in a form, I were assuming that this form would also be submitted, because that's how data in forms usually gets to a PHP script.

A small example:



<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="val1" value="10" /> 10 <br />
<input type="checkbox" name="val2" value="20" /> 20 <br />
<input type="checkbox" name="val3" value="30" /> 30 <br />
<input type="submit" />
</form>

<?php
if (count($_POST) > 0) {
var_dump($_POST);
}
?>


Getting a checkbox value is pretty simple because every box that has been checked is visible in the $_POST array in the receiving PHP script - that means the page to where the form is submitted. In the above example, we just submit the form to itself, i.e. the same page.
var_dump is a tremendously helpful method for determining the type and values of a variable, in this case it tells us what variables were submitted by the form.

Does that help?

Jason
03-07-2003, 12:13 AM
that helps alot, but what I don't get now is where the var_dump($_POST) will get displayed on the submit. And that <input type= "submit" /> is that a button, cause I thought I had to decalre it as a button.

so I have
'
document.writeln ( '<INPUT TYPE="button" VALUE="Update" ONCLICK="update_office_hours()">' );
document.writeln ( '<INPUT TYPE="button" VALUE="Uncheck All" ONCLICK="uncheck_all(this.form)">' );
document.writeln ( '<INPUT TYPE="button" VALUE="Check All" ONCLICK="check_all(this.form)">' );
</SCRIPT>

<NOSCRIPT>
<INPUT TYPE="submit" VALUE="Update">
</NOSCRIPT>

<?php
if (count($_POST) > 0) {
var_dump($_POST);
}
?>
'
How can I see what is produced in the $_POST array since right now it isn't showing me anything when I do a echo $_POST[0]; or echo var_dump($_POST).
What am I missing?

Jason

Jason
03-07-2003, 12:20 AM
Ok, I see my error. Now I have a sql problem. Thanks

Jason

Jason
03-07-2003, 12:28 AM
I think I figured it all out...thanks mordred

mordred
03-07-2003, 12:28 AM
Originally posted by Jason
that helps alot, but what I don't get now is where the var_dump($_POST) will get displayed on the submit.

Right where it is written - var_dump() prints it's message directly to the output.



And that <input type= "submit" /> is that a button, cause I thought I had to decalre it as a button.


I would call it a submit button, I don't understand why you are asking if you should declare it as a button... a submit button is a very elementary part of every HTML form... maybe I'm missing sth. now.



document.writeln ( '<INPUT TYPE="button" VALUE="Update" ONCLICK="update_office_hours()">' );


Hm. Why are you writing this with JavaScript? Putting it in the HTML source directly would have the same effect plus that people with disabled JS could also use your form.



How can I see what is produced in the $_POST array since right now it isn't showing me anything when I do a echo $_POST[0]; or echo var_dump($_POST).


You just write var_dump(), not echo var_dump() - as stated above var_dump() prints directly to the output. As to why your POST variables don't show up there may be a variety of reasons:

- Wrong URL in action attribute of form tag
- Form submit method is not POST
- You run PHP < PHP4.1, in this case you need to use $HTTP_POST_VARS.

Jason
03-07-2003, 01:31 AM
Ok, I get array(1) { [""]=> string(15) "" } as my sample output but the string looks blank. Is there a way to isolate the string and print it seperately?

I also changed my dynamic checkbox creation to :
echo "<INPUT TYPE=\"checkbox\" NAME=\"<?php $work_day_start_time$min ; ?>\" VALUE=\"<?php $work_day_start_time$min ; ?>\" "
. (($ver_ans == 1)?"CHECKED":"UNCHECKED") . "> " . "$display_time";


Jason

mordred
03-07-2003, 01:27 PM
That's a rather odd way of putting strings together, it produces input field names like <?php timeValMinVal ; ?>. I think you're doing something wrong here, and rather want this:



echo "<INPUT TYPE=\"checkbox\" NAME=\"$work_day_start_time$min\" VALUE=\"$work_day_start_time$min\" "
. (($ver_ans == 1)?"CHECKED":"UNCHECKED") . "> " . "$display_time";

Jason
03-07-2003, 10:36 PM
Thanks mordred, I think I was getting parse errors so I kept adding <?php ?> crap until I didn't not realizing that I was screwing the code up more. Thanks again, that works and outputs the string contents and everything.

What is the syntax for the $_POST array? like if I wanted to go through each element?
Here is sample output
array(1) { [1745]=> string(4) "1745" }
and it will always be a string of numbers, how can I get the numbers or the string of numbers?

Jason

Jason
03-08-2003, 12:02 AM
K, I got it all...Thanks to he who helped.

Jason



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum