Jump to content

failing to declare a varible from an lower positon


Go to solution Solved by AzeS,

Recommended Posts

i try to declare some vars during an msg sending process but it wont work any sugesstions ?

Code snippets:

Vars:
 

$ID = $userRow['userId'];
$cond=0;
$CBA=0;
global $SENDER, $RECIVER, $SESSID;

Error providing code:

if (isset($_POST['smsg'])) {
  $OBJECT = strip_tags($_POST['smsginp']);
  $OBJECT = trim($OBJECT);
  $OBJECT = str_split($OBJECT, 255);
  if (isset($OBJECT[1])) {
    $res = $crud->sendmsg($SENDER,$RECIVER,$OBJECT[0],$OBJECT[1],$SESSID,0);
  } else {
    $res = $crud->sendmsg($SENDER,$RECIVER,$OBJECT[0],null,$SESSID,0);
  }
    if ($res != 0 ) {
      $msg = "Your message has been sended succesfully.";
    } else {
      $msg = "there was been an error sending your message you might report a bug.";
    }
}
if (isset($_POST['sdestroy'])) {
  $res = $crud->sendmsg($SENDER,$RECIVER,"closing the session",null,$SESSID,0);
      if ($res != 0 ) {
      $msg = "session has been closed we hope we could help you with your problem";
    } else {
      $msg = "there was been an error closing the session you might report a bug.";
    }
}

Declaring code:
 

    <?php 
      $cond = $crud->check($ID);
      if ($cond > 0) {
        $ROWS_SESS = $crud->THOUTSESS($ID);
        $SESSID = $ROWS_SESS['SESSID'];
        $SENDER = $ID;
        $RECIVER = $ROWS_SESS['FROM_'];
        echo "THIS SESSID CURRENT__:" . $SESSID . "<br>";
        echo "THIS SENDER CURRENT__:" . $SENDER . "<br>"; 
        echo "THIS RECIVER CURRENT_:" . $RECIVER . "<br>"; 
        $CBA = 1;
        $smsgtoutall = $crud->getmsg($SESSID);
        if ($smsgtoutall == 0 ) {
            echo "<li><p>there are currently no items to display yet..." . "</p></li>";
        } else {
          foreach($smsgtoutall as $row) { 
            if ($row['FROM_'] == $ID) {
              $SENDERN = "You";
            } else {
              $SENDERN = "Employe";
            }
            echo "<li><p>" . $crud->word($SENDERN, 'UTF-8') . ": " . $crud->word($row['SUBJECT'], 'UTF-8') . $crud->word($row['SUBJECT_2'], 'UTF-8') . "</p><p> " . $crud->word($row['TIMEST'], 'UTF-8') . "</p></li>";
          }
        }

giphy.gif

Edited by AzeS

What matters is the relationship between all that code you've posted. Yeah, sure, you have something that defines those variables, but is it executing before everything else? Are the variables defined in the global scope like your code seems to expect them to be?

 

Post all the code at once. Whole files, not just a couple blocks from them.

its huge thats wy i didnt postet all code...

but here we go:

Ajax.js for updating the specified div :
 

var timeout = setTimeout(reloadChat, 5000);
 
function reloadChat () {
    $('#chatcz').load('Help.php #chatcz',function () {
         $(this).unwrap();
         timeout = setTimeout(reloadChat, 5000);
    });
}

send msg code :

public function sendmsg($FROM,$TO,$SUB1,$SUB2,$SESSID,$END) {
	#csessionschat
	try {
	$sql = $this->Db->prepare("INSERT INTO csessionschat(FROM_,TO_,SUBJECT,SUBJECT_2,SESSID,ENDSESS) VALUES (:f,:t,:s1,:s2,:si,:e)");
	$sql->execute(array(':f' => $FROM, 
						':t' => $TO, 
						':s1' => $SUB1,
						':s2' => $SUB2,
						':si' => $SESSID,
						':e' => $END));
	if ($END != 0) {
		$this->killsess($SESSID);
	}
 	} catch (PDOException $ex) {
		echo $ex->getMessage();
		return 0;
	}


}

Help.php:

 

<?php
 #error_reporting( ~E_NOTICE );
 ob_start();
 session_start();
 require_once '../dbconnect.php';
 if( !isset($_SESSION['user']) ) {
  header("Location: index.php?reqlog");
  exit;
 }

$userRow=$crud->getuser($_SESSION['user']);
$ID = $userRow['userId'];
$cond=0;
$CBA=0;
global $SENDER, $RECIVER, $SESSID;
if (isset($_POST['submit'])) {
  $BLOCK = $userRow['BLOCK'];
  $MAIL = $userRow['userEmail'];
  $TAG = strip_tags($_POST['tag']);
  $TAG = trim($TAG);
  if ($BLOCK > 0) {
    $msg = "Your Blocked for this service " . $crud->word($userRow['UzRI'], 'UTF-8') . " <br>why dont you check on your emails and see the reason why; for taking some further aktions and getting around, faced to this fact? <br>or maybe you contact us via email at: support@(NAME_).com for some help to get arround this problem.";
  } else {
    if (!empty($_POST['reason']) || $TAG != "Please select a tag.") {
    $rs = strip_tags($_POST['reason']);
    $rs = trim($rs);
    $rs = str_split($rs, 255);
    if (isset($rs[1])) {
      $res = $crud->sendticket($ID,$rs[0],$rs[1],$TAG,$MAIL);
    } else {
      $res = $crud->sendticket($ID,$rs[0],null,$TAG,$MAIL);
    }
    
    if ($res != 0 && $res != 2) {
      $msg = "Your request for help have been succesfully submited " . $crud->word($userRow['UzRI'], 'UTF-8') . ".";
    } else {
      if ($res == 2) {
       $msg = "You already submitted a ticket " . $crud->word($userRow['UzRI'], 'UTF-8') . " wy dont you wait a little more? An service providing employe will be there for you soon. <br>Remember, by leaving this page your ticket will be deleted.";
      } else {
        $msg = "There was been an error " . $crud->word($userRow['UzRI'], 'UTF-8') . ". Maybe you try it later again";
      }
    }
    
  } else {
    $msg = "All inputs must be filled for sending your help request to us " . $crud->word($userRow['UzRI'], 'UTF-8') . ".";
  }
  }

}

if (isset($_POST['smsg'])) {
  $OBJECT = strip_tags($_POST['smsginp']);
  $OBJECT = trim($OBJECT);
  $OBJECT = str_split($OBJECT, 255);
  if (isset($OBJECT[1])) {
    $res = $crud->sendmsg($SENDER,$RECIVER,$OBJECT[0],$OBJECT[1],$SESSID,0);
  } else {
    $res = $crud->sendmsg($SENDER,$RECIVER,$OBJECT[0],null,$SESSID,0);
  }
    if ($res != 0 ) {
      $msg = "Your message has been sended succesfully.";
    } else {
      $msg = "there was been an error sending your message you might report a bug.";
    }
}
if (isset($_POST['sdestroy'])) {
  $res = $crud->sendmsg($SENDER,$RECIVER,"closing the session",null,$SESSID,0);
      if ($res != 0 ) {
      $msg = "session has been closed we hope we could help you with your problem";
    } else {
      $msg = "there was been an error closing the session you might report a bug.";
    }
}
?>
<!DOCTYPE html>
<html>



<HEAD>
  <link rel="stylesheet" href="../css/style.css">
  <link rel="stylesheet" href="../css/normalize.css">
  <title>Welcome - <?php echo $userRow['userEmail']; ?></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script type="text/javascript" src="../js/ajax.js"></script>
  <script>
function textCounter(field,field2,maxlimit)
{
 var countfield = document.getElementById(field2);
 if ( field.value.length > maxlimit ) {
  field.value = field.value.substring( 0, maxlimit );
  return false;
 } else {
  countfield.value = maxlimit - field.value.length;
 }
}

</script>
  </HEAD>
  <BODY>
  <div class="HeaderC" >
    <DIV class="Header">
      <header class="Header clearfix">
      <a class="logo" href="../index.php">
        <img src="../IMG/LAY/Logo.png" height="85" alt="">
      </a>
      <nav>
***
      </nav>  
      </header>
    </DIV>
    </form>
    ***
  
  
  </div>  
  <div class="Space_0 Background_0"></div>



<div class="Space_0 Background_1"></div>

<div class="Space_0 Background_1"></div>

<div class="CZC0">
  <div class="CZ0">
    <form method="POST" class="CZT">
      <center>You have <input disabled  maxlength="3" size="1" value="510" id="counter"> Characters left</center>
      <textarea placeholder="Please describe what exactly went wrong, after submitting your problem we will be there for you as fast as we can. Usualy it takes around 1 to 15 minutes." name="reason" cols="110" rows="10" maxlength="510" onkeyup="textCounter(this,'counter',510);" id="message" ></textarea>
      <select name="tag">
                <option>Please select a tag.</option>
                <option>REASON 1</option>
                <option>REASON 2</option>
                <option>REASON 3</option>
                <option>REASON 4</option>
                <option>OTHER</option>
      </select> 
      <button name="submit">Submit</button>
    </form>
  </div>
</div>

<div class="CZC1">
  <div class="CZ1">
    <?php 
    if (isset($msg)) {
      echo $msg;
    }
    ?>
  </div>
  <div class="HeZ Body_E01" id="chatcz">
    <ul>
    <?php 
      $cond = $crud->check($ID);
      if ($cond > 0) {
        $ROWS_SESS = $crud->THOUTSESS($ID);
        $SESSID = $ROWS_SESS['SESSID'];
        $SENDER = $ID;
        $RECIVER = $ROWS_SESS['FROM_'];
        echo "THIS SESSID CURRENT__:" . $SESSID . "<br>";
        echo "THIS SENDER CURRENT__:" . $SENDER . "<br>"; 
        echo "THIS RECIVER CURRENT_:" . $RECIVER . "<br>"; 
        $CBA = 1;
        $smsgtoutall = $crud->getmsg($SESSID);
        if ($smsgtoutall == 0 ) {
            echo "<li><p>there are currently no items to display yet..." . "</p></li>";
        } else {
          foreach($smsgtoutall as $row) { 
            if ($row['FROM_'] == $ID) {
              $SENDERN = "You";
            } else {
              $SENDERN = "Employe";
            }
            echo "<li><p>" . $crud->word($SENDERN, 'UTF-8') . ": " . $crud->word($row['SUBJECT'], 'UTF-8') . $crud->word($row['SUBJECT_2'], 'UTF-8') . "</p><p> " . $crud->word($row['TIMEST'], 'UTF-8') . "</p></li>";
          }
        }

      } else {
        echo $crud->check($ID);
        echo "<br>this: " . $cond;
      }
    ?>
    </ul>
  </div>
  <div class="CZ3">
    <form method="POST" class="CZT">
            <center>You have <input disabled  maxlength="3" size="1" value="510" id="counter2"> Characters left</center>
      <textarea placeholder="Message..." maxlength="510" name="smsginp" cols="55" rows="5" onkeyup="textCounter(this,'counter2',510);" id="message" >
        
      </textarea>
      <button name="smsg">Send</button> 
      <button name="sdestroy">End Session</button> <p>Or</p>
      <button name="report">Report abuse</button>
      
    </form>
  </div>



</div>


 

Edited by AzeS

Which of the variables isn't defined? This is too much code for guessing games.

 

Besides that, you've adopted a very strange programming style. I'm not sure if you're trying to write VB.NET code in PHP, but it doesn't work like this:

  • You do not “declare” variables in PHP on top of the script. You define them when they're needed (if they're needed at all).
  • What's the matter with those all-uppercase variables? In PHP, variables are all-lowercase.
  • Don't write one big block of PHPSQLHTMLCSSJavaScript spaghetti code. Keep the languages separate: PHP at the top, HTML at the bottom, JavaScript in external files, CSS in external files. Then we don't have to wade through hundreds of lines of irrelevant code for one simple problem.
  • Indentation is 4 spaces.
  • Your strange CRUD instance is turning into a God Object which does way, way too much (queries, HTML-escaping, sending messages and who knows what else). You realize that you can have more than one class, right?
Edited by Jacques1

giphy.gif
yes i do realiz that  : 


but in this case i need to update an specific block of php code so that all the data gets frequently displayed, in this case all five seconds.
and yes i have a hard time learning php but i will keep my style as i do so in vb...

so mention following :


i have a var on a function that needs to be declared so it can be returned:
 

public function bla($var2) {
return $var2;
}

and i have a function that needs to be updatet every single 5 seconds
 

magic five seconds code as an single thread....

code thats need to be updatet
 

$var2 = "bla"; 

and now i have an event that gives the var to the function.

 

$this->bla($var2);

but the problem is that var2 is still null.. :suicide:

 

 

Edited by AzeS

You cannot “update a block of PHP code” every 5 seconds.

 

As far as I understand, you're trying to implement some kind of chat with pure PHP. This is impossible, because PHP produces a single HTTP response to a single request. When the response has been sent to the client, you cannot go back to change it. If you want a dynamically change the page, you need JavaScript or more specifically Ajax.

 

So the whole chat block doesn't make any sense in the first place.

  • Solution

!!! SOLVED !!!

What was i doing wrong ? 
as i early mentioned i tried to "move a variable upwards" this seems to be impossible for php at least nobody had a suggestion so 

my scraped up mind did came up with an great idea!!  ::) 
 

wy not move them downwards and lift them with the post method... i know it seems obvios but as leazy and sadistic i am i tried to ask the community for help ... but enough of my stupiditie and see what i did wrong...
corrected code below:

EVENT: 

if (isset($_POST['smsg'])) {
  $OBJECT = strip_tags($_POST['smsginp']);
  $OBJECT = trim($OBJECT);
  $OBJECT = str_split($OBJECT, 255);
  $SENDER = strip_tags($_POST['SESSID']);
  $SENDER = trim($SENDER);
  $RECIVER = strip_tags($_POST['RECIVER']);
  $RECIVER = trim($RECIVER);
  $SESSID = strip_tags($_POST['SESSID']);
  $SESSID = trim($SESSID);
  echo $SESSID;
  if (isset($OBJECT[1])) {
    $res = $crud->sendmsg($SENDER,$RECIVER,$OBJECT[0],$OBJECT[1],$SESSID,0);
  } else {
    $res = $crud->sendmsg($SENDER,$RECIVER,$OBJECT[0],null,$SESSID,0);
  }
    if ($res != 0 ) {
      $msg = "Your message has been sended succesfully.";
    } else {
      $msg = "there was been an error sending your message you might report a bug.";
    }
}
if (isset($_POST['sdestroy'])) {
  $res = $crud->sendmsg($SENDER,$RECIVER,"closing the session",null,$SESSID,0);
      if ($res != 0 ) {
      $msg = "session has been closed we hope we could help you with your problem";
    } else {
      $msg = "there was been an error closing the session you might report a bug.";
    }
}

method: 

  <div class="HeZ Body_E01" id="chatcz">
    <ul>
    <?php 
      $cond = $crud->check($ID);
      if ($cond > 0) {
        $ROWS_SESS = $crud->THOUTSESS($ID);
        $SESSID = $ROWS_SESS['SESSID'];
        $SENDER = $ID;
        $RECIVER = $ROWS_SESS['FROM_'];
        echo "THIS SESSID CURRENT__:" . $SESSID . "<br>";
        echo "THIS SENDER CURRENT__:" . $SENDER . "<br>"; 
        echo "THIS RECIVER CURRENT_:" . $RECIVER . "<br>"; 
        $CBA = 1;
        $smsgtoutall = $crud->getmsg($SESSID);
        if ($smsgtoutall == 0 ) {
            echo "<li><p>there are currently no items to display yet..." . "</p></li>";
        } else {
          foreach($smsgtoutall as $row) { 
            if ($row['FROM_'] == $ID) {
              $SENDERN = "You";
            } else {
              $SENDERN = "Employe";
            }
            echo "<li><p>" . $crud->word($SENDERN, 'UTF-8') . ": " . $crud->word($row['SUBJECT'], 'UTF-8') . $crud->word($row['SUBJECT_2'], 'UTF-8') . "</p><p> " . $crud->word($row['TIMEST'], 'UTF-8') . "</p></li>";
          }
        }

      } else {
        echo $crud->check($ID);
        echo "<br>this: " . $cond;
      }
    ?>
    </ul>
  </div>
  <div class="CZ3">
    <form method="POST" class="CZT">
            <center>You have <input disabled  maxlength="3" size="1" value="510" id="counter2"> Characters left</center>
            <div id="chatcz">
      <input type="text" name="SESSID" value="<?php echo $crud->word($SESSID, 'UTF-8') ?>">
      <input type="text" name="SENDER" value="<?php echo $crud->word($SENDER, 'UTF-8')?>">
      <input type="text" name="RECIVER" value="<?php echo $crud->word($RECIVER, 'UTF-8')?>">
            </div>

      
      <textarea placeholder="Message..." maxlength="510" name="smsginp" cols="55" rows="5" onkeyup="textCounter(this,'counter2',510);" id="message" >
        
      </textarea>
      <button name="smsg">Send</button> 
      <button name="sdestroy">End Session</button> <p>Or</p>
      <button name="report">Report abuse</button>
      
    </form>
  </div>

giphy.gif

Sincerly, AzeS.

P.S.


#WORKING PHP CHAT APP

Edited by AzeS
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.