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>
ucohockeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Neil_BradleyWeb UX/UI DeveloperCommented:
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

0
ucohockeyAuthor Commented:
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
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

ucohockeyAuthor Commented:
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.
0
StingRaYCommented:
Try this:

<?php
function Validator($origValue){
  $number = preg_replace('/[^0-9]+/', '', $origValue);
  $number = preg_replace('/^(9.*)/', '0$1', $number);
  echo "# = " . $number;
}

echo Validator("dd980498");
?>

Open in new window


The output is:

# = 0980498

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ucohockeyAuthor Commented:
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.
0
StingRaYCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.