Link to home
Start Free TrialLog in
Avatar of ucohockey
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(number).value;
  //alert('start=' + origValue);
  number = origValue.replace(/[^0-9]+/g, '');
  number = number.replace(/^(9.*)/, '0$1');
  alert("# = " + number);
}

</script>
Avatar of Neil_Bradley
Neil_Bradley
Flag of New Zealand image

Try this.. I have used a bachslash to escape from any quotaion marks inside the java.
Cheers,
N
<?php
echo "
<script language=\"javascript\" type=\"text/javascript\">
function Validator(number){
  var origValue = document.getElementById(number).value;
  //alert('start=' + origValue);
  number = origValue.replace(/[^0-9]+/g, '');
  number = number.replace(/^(9.*)/, '0$1');
  alert(\"# = \" + number);
}

</script> ";
?>

Open in new window

Avatar of ucohockey
ucohockey

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
Avatar of Dave Baldwin
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.
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
Avatar of StingRaY
StingRaY
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
StingRaY thanks for the help but still not working, I was using var origValue = document.getElementById(number).value; 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:

<form ... method="POST">
<input type="text" name="number" id="number">
</form>

Open in new window


Then you get the value of "number" by:

$number = $_POST["number"];

// and then validate it or whatever you want

echo Validator($number);

Open in new window