nasty
nasty

Reputation: 7077

Check if a characters occurs once in URL

My Question

I want to check if the URL contains one ? if true, do something

My PHP

Im using this script currently.

$lp_uri = $_SERVER['REQUEST_URI']; // or taken from another source //
    if( strpos($lp_uri, '?') !== false ){
    echo '<script>alert("one question mark");</script>';
}

My Issue

Im not sure how to check if the? occurs once only. Can you please help? Thanks heaps

The reason

I have a contact form on my website and once someone submits it, it addds a parameter to the url so it look like website.com?msgsent=1. This means the message has been send. But sometimes if the email field is incorrect, it still adds a ? to the url so the url looks like website.com?.

Now, I am using a jQuery slideDown delay on my contact form to grab customers attention and everytime the page refreshes, it takes few seconds to slideDown. I want to overcome this if the url has a ? in it because if the url looks website.com? that means, the email address is wrong.

Hope this makes sense.

Upvotes: 0

Views: 228

Answers (2)

s3v3n
s3v3n

Reputation: 8446

The solution for the initial question (just in case someone will find this question on google) is to use substr_count:

if(substr_count($text, '?') == 1){
    // do something
}

The solution to your problem would be to check if the msgsent parameter is set:

if(!isset($_GET['msgsent'])){
    // message not sent - do something
}

Upvotes: 4

Philip Whitehouse
Philip Whitehouse

Reputation: 4157

A possible solution making use of the offset optional parameter

<?php
if( strpos($lp_uri, '?') !== false ){ 
   //1 or more question marks
   $pos = strpos($lp_uri, '?');  //Grab the position of it
   if(strpos($lp_uri, '?',$pos) === false) {
      //No more after the first, hence only 1
   }
}

Upvotes: 0

Related Questions