sammy
sammy

Reputation: 717

send if button clicked PHP SQL

i'm trying to show notice to user if information sent property:

"نظر شما با موفقیت ثبت شد"

or else show notice if he or she try to click on button when fields are empty:

"لطفا فیلدهای خالی را پر کنید"

but always it show me 'else' part when i run my page.

<?php
if(isset($_POST['send_shekayat']))
{
  if(!empty($_POST['body']) AND !empty($_POST['title']))
    {
    $user_id =$fgmembersite->UserID();

       $db_host = 'localhost';
       $db_name= 'site';
       $db_table= 'shekayat';
       $db_user = 'root';
       $db_pass = '';

    $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

    mysql_query("SET NAMES 'utf8'", $con);
    mysql_query("SET CHARACTER SET 'utf8'", $con);
    mysql_query("SET character_set_connection = 'utf8'", $con);

    $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
    mysql_query("SET CHARACTER SET  utf8");
    $ins="INSERT INTO $db_table (user_id,title,body) 
                VALUES('$user_id','" . mysql_escape_string($_POST['title']) . "','" . mysql_escape_string($_POST['body']) . "')";
    $saved=mysql_query($ins );
    mysql_close($con); 
    echo '<script language="javascript">';
    echo 'alert("نظر شما با موفقیت ثبت شد")';
    echo '</script>';
    echo '<script>window.location.href = "submit_pishnahad.php";</script>';

    }
}
else
{
    echo '<script language="javascript">';
    echo 'alert("لطفا فیلدهای خالی را پر کنید")';
    echo '</script>';
    echo '<script>window.location.href = "submit_pishnahad.php";</script>';
}
?>

And here is one textbox one textfield and one button to send:

<form name="form2" method="post" action="" accept-charset='UTF-8'>

<div class='container' dir="rtl" >
    <label for='title'>موضوع: </label>
    <input type='text' name='title' id='title'/><br/>

</div>

<div class='container' dir="rtl" >
    <label for='body'>توضیحات: </label> <br />
    <textarea name="body" id="body" cols="" rows="" style="width:300 ;height:300"></textarea>

</div>

<div class='container'>
    <input type='submit'  name='send_shekayat' value='ارسال اطلاعات' />

</div>
</form>

Upvotes: 1

Views: 66

Answers (3)

sammy
sammy

Reputation: 717

i have to close if tag after else tag for working property:

<?php
if(isset($_POST['send_shekayat']))
{
    if(!empty($_POST['body']) AND !empty($_POST['title']))
    {
         $user_id =$fgmembersite->UserID();
         $db_host = 'localhost';
         $db_name= 'site';
         $db_table= 'shekayat';
         $db_user = 'root';
         $db_pass = '';

    $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

    mysql_query("SET NAMES 'utf8'", $con);
    mysql_query("SET CHARACTER SET 'utf8'", $con);
    mysql_query("SET character_set_connection = 'utf8'", $con);

    $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
    mysql_query("SET CHARACTER SET  utf8");
    $ins="INSERT INTO $db_table (user_id,title,body) VALUES('$user_id','" . mysql_escape_string($_POST['title']) . "','" . mysql_escape_string($_POST['body']) . "')";
    $saved=mysql_query($ins );
    mysql_close($con); 
    echo '<script language="javascript">';
    echo 'alert("نظر شما با موفقیت ثبت شد")';
    echo '</script>';
    echo '<script>window.location.href = "submit_pishnahad.php";</script>';

  }
  else
  {
    echo '<script language="javascript">';
    echo 'alert("لطفا فیلدهای خالی را پر کنید")';
    echo '</script>';
    echo '<script>window.location.href = "submit_pishnahad.php";</script>';
   }
}?>

Upvotes: 0

Shahid Ahmad
Shahid Ahmad

Reputation: 774

use this

if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['send_shekayat']))

Upvotes: 0

Nana Partykar
Nana Partykar

Reputation: 10548

Try this. This working fine for me. Hope it works for you also.

<?php
if(isset($_POST['send_shekayat']))
{
  if(!empty($_POST['body']) || !empty($_POST['title']))
    {
        .
        . //Your Code
        .
        echo '<script language="javascript">';
        echo 'alert("Value Entered")';
        echo '</script>';
        echo '<script>window.location.href = "submit_pishnahad.php";</script>';
    }
    else
    {
        .
        .
        echo '<script language="javascript">';
        echo 'alert("Value Not Entered")';
        echo '</script>';
        echo '<script>window.location.href = "submit_pishnahad.php";</script>';
    }
}
?>

Upvotes: 1

Related Questions