ucohockey
asked on
I have some javascript and I have to have it inside some php code how to do it?
I've gotten help on some javascript but just realized it needs to be in php or inside php for it to work. I have a form that I need to change some of the inputs and was able to do so with javascript but the only place form me to place this is inside php. Can you help. Below is the javascript I'm using.
<script language="javascript" type="text/javascript">
function Validator(number){
var origValue = document.getElementById(nu mber).valu e;
//alert('start=' + origValue);
number = origValue.replace(/[^0-9]+ /g, '');
number = number.replace(/^(9.*)/, '0$1');
alert("# = " + number);
}
</script>
<script language="javascript" type="text/javascript">
function Validator(number){
var origValue = document.getElementById(nu
//alert('start=' + origValue);
number = origValue.replace(/[^0-9]+
number = number.replace(/^(9.*)/, '0$1');
alert("# = " + number);
}
</script>
ASKER
Thanks for the help but I don't think I explained it well. I'm not able to call the javascript from the form I need it placed with in the php that runs after the form is submitted.
thanks
thanks
Javascript only runs in the browser, not on the server with PHP. Typically, a validation javascript is run by calling it with an 'onsubmit' command in the <form> tag. Here http://www.w3schools.com/js/js_form_validation.asp are some examples.
ASKER
Ok if I can't do that then how do I take what I'm doing and use php instead of javascript. I can't access the submit button or the form but I have access to the php that post the form.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
StingRaY thanks for the help but still not working, I was using var origValue = document.getElementById(nu mber).valu e; do I need to say where or what I'm trying to get.
My post above is PHP code, not javascript. You cannot access origValue by document.getElementById but $_POST or $_GET depending on page request method (POST or GET).
For example, if your form look like this:
Then you get the value of "number" by:
For example, if your form look like this:
<form ... method="POST">
<input type="text" name="number" id="number">
</form>
Then you get the value of "number" by:
$number = $_POST["number"];
// and then validate it or whatever you want
echo Validator($number);
Cheers,
N
Open in new window