Forum Moderators: coopster

Message Too Old, No Replies

Collecting data from input failure

Accessing php in js to collect data from a form

         

nesohc

9:12 am on Oct 13, 2009 (gmt 0)

10+ Year Member



Hey! Im trying to collect data from an input type text with POST method inside a javascript. the JS is not the problem, the php part is as far as I can tell.

There is way too much code to post it all, but here is what im trying to do:

function js_function()
{
<?php
// I'm trying to echo an alert to see that my values are correct, I need to use this number later on in the script.
$number = $_POST['top_number'];
echo 'alert("' . $number . '");';

// This part is working and showing as it should with alert
$testing = 400;
echo 'alert ("' . $testing . '");';
?>
}

html…

<form name="myForm" method="post" onSubmit="js_function()">
<input type="image" src="mypic.jpg" width="45" height="45" value="submit">
<input name="top_number" type="text">
</form>

The $number value is returning nothing, when I alert it I dont get any number at all. Even the input text part contains a value. I've tried using both:
POST[ 'text' ] and POST[ "text" ] seems to do no difference at all. I'm not sure if the collection actually provides me with the correct info, or if I'm just trying to collect it from post incorrectly.

Regards, Markus

andrewsmd

1:33 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try doing a var_dump($_POST); to see all of your post data. If you alert is echoing nothing then that means that $_POST['top_number'] is nothing. It's hard to help pinpoint your problem if we don't have more code so I'm just guessing here.

idfer

4:54 pm on Oct 13, 2009 (gmt 0)

10+ Year Member



nesohc, the PHP code you have is executed before the page is sent to the browser and at that point $_POST is most likely empty, so the resulting Javascript code will always look like this:

function js_function()
{
alert("");
alert ("400");
}

If you're trying to validate the user data before submitting it to the server, you'll need to use purely Javascript code to do that. Search the net for "javascript form validation" for lots of good examples.