Libphonenumber-for-php

Hi everyone

I have loaded libphonenumber for php and also I have created variable which is $phone that I would from users to enter their info as input type. Also, I would from libphonenumber that can check the $phone is (valid/invalid) and it can loading area code …etc

<?php
require __DIR__ . '/../vendor/autoload.php';


?>


<!DOCTYPE html>
<html>
<head>
	<title>welcom to register.php</title>
</head>
<body>

<form action="welcome.php" method="$_POST">
<div>
<p> <label>Enter your phone</label>
<input type="text" name="$phone" placeholder="Enter your phone number">
	</p>
	</div>
	<input type="submit" value="Submit">
	
</form>
<?php 

header("location: welcome.php"); 

?>
</body>
</html>

Thanks for your help

I would suggest to start actually using the lib and posting relevant code if you encounter any problem.

Yes, you haven’t really asked anything there. The idea of the forum is that you try to get it working, and if you run into trouble, come along and ask specific questions to find out why it’s not working. At the moment, you’ve said roughly what you want to do, but don’t seem to have done any of the work. Are there any samples you can run through from the provider of the library, to get you started?

Also, there’s a typo on this line:

<form action="welcome.php" method="$_POST">

And this section of code:

<?php 

header("location: welcome.php"); 

?>

won’t execute because you’ve already sent output to the browser. You should be getting a “headers already sent” error when you try to run this.

And another minor typo:

<input type="text" name="$phone" placeholder="Enter your phone number">

Hint: You’re mixing up PHP variable names and html attributes in these two typos.

1 Like

I’ll
thank you @chorn

Thank you @droopsnoot

I have made two php files which are test.php and welcome.php
as it has been shown to you in code lines.
I do not have any idea what I must write code in or to load all country codes from libphonenumber-for-php

-test.php

<?php
require __DIR__ . '/../vendor/autoload.php';

$phone_err="";
$phone = "";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();

if ($_SERVER["REQUEST_METHOD"] == "POST") {

	if  (empty(trim($_POST["phone"]))) { 
          $phone_err = "Please enter your phone number. ";
    } else {
try {
    $phone_err = $phoneUtil->parse($phone, "CH");
	var_dump($phone_err);
} catch (\libphonenumber\NumberParseException $e) {
    var_dump($e);
}
	 
if ($phone < 10){
	$phone_err ="You must enter 10 digit numbers";
	
}else{
	$isValid = $phoneUtil->isValidNumber($phone_err);
	var_dump($isValid);
}
}
}
?>

and welcome.php


<?php
require __DIR__ . '/../vendor/autoload.php';
include_once'test.php';

?>

<!DOCTYPE html>
<html>
<head>
	<title>welcom to register page</title>
</head>
<body>

<form action="welcome.php" method="POST">

<div>
<p> 
		<label>Enter your phone</label>
		<br>
		<label> Country code </label>
		<select name="countrycodee" >
		<..! I do not have any idea what I must write code in or to load all country codes from libphonenumber-for-php..>
             <option> </option>
        
		</select>
		<input type="text" name="phone" placeholder="Enter your phone number" value="<?php echo $phone;?>">
		<span> <?php echo $phone_err;?> </span>
		
</p>
</div>
		<input type="submit" value="Submit">
			
</form>

</body>
</html>

any help would be appreciated,
Thank you.

Before thinking about making the form submission work, get the form right first, there are a few mistakes that stand out immediately.

When you set the methos to post in HTML you don’t use the PHP global, just post:-

<form action="welcome.php" method="post">

Likewise on your input, it is HTML, not PHP so you don’t use a PHP variable there.

<input type="tel" name="phone" placeholder="Enter your phone number">

You can also use the tel input type in HTML5 for phone numbers.

Where you have a label for an input you should assign the label to the input with the for attribute with a value matching the input id attribute value.

<label for="phone">Enter your phone</label>
<input type="tel" name="phone" id="phone" placeholder="Enter your phone number">

Lastly this makes no sense here.
You can’t send headers after there has been any output to the browser. There is no need to forward the user to the welcome.php page, as you already have that as your form action.

Again some confusion. If test.php is parsing the form, it should be in the form’s action attribute.
But if welcome.php is supposed to be the action, the same file that the form is in, then you can leave the action blank or enter # for it.

Some of the form parsing php doesn’t make sense either, but one step at a time, get the form right first.

1 Like