undefined index error

PHP Notice – yii\base\ErrorException

Undefined index: form_id




 public function actionAlterfield(){

	

    	$this->layout = false;

    	[b]$form_id = (int)$_POST['form_id'];[/b]

    	$element_id	=  isset($_POST['element_id']) ? $_POST['element_id'] : '';

    	$element_type = trim($_POST['element_type']);

    	return $this->render('alterfield',array('form_id'=>$form_id,'element_id'=>$element_id,'element_type'=>$element_type));

    }



some one please help to solve this bug.

We cant help without further information.

Have you checked if form_id is in $_POST?




echo var_dump($_POST);



Regards

Here is further code




<?php


use yii\web\View;

use yii\helpers\Url;

use yii\helpers\Html;




//$this->renderPartial('common');

$connection = Yii::$app->db;	







	if($element_type == 'page_break'){

		//exception for page break, we can ignore the element status, live or draft field can be deleted immediately

		$query  = "delete from `form_elements` where form_id = '{$form_id}' and element_id='{$element_id}'";

	    $command = $connection->createCommand($query);

	    $command->query();

			

		//after deleting the page break, we need to recalculate the page number field for all existing live field on the form

		$query = "SELECT 

						element_id,element_position 

					FROM 

						form_elements 

				   WHERE 

				   		form_id = '{$form_id}' and element_type='page_break' and element_status=1 

				ORDER BY 

						element_position asc";

		$command = $connection->createCommand($query);

	    $page_brake_elements = $command->queryAll();

		

		

						

						

		$page_number = 1;

		if(count($page_brake_elements) > 0){

			foreach($page_brake_elements as $ele){

				$page_break_list[$page_number] = $ele->element_position;

				$page_number++;

			}

		}

		

		$total_page = $page_number;

		if(!empty($page_break_list)){

			krsort($page_break_list);

		}

		

		//set the page number of all fields to the highest page number

		$query = "UPDATE 

						form_elements 

					 SET 

						element_page_number = '{$total_page}'

				   WHERE

					    form_id = '{$form_id}' and (element_status = '1' or element_status = '2') ";

		$command = $connection->createCommand($query);

	    $command->query();

		

		//then loop through each page break and set the page number of all fields below that page break

		if(!empty($page_break_list)){

		

			foreach ($page_break_list as $page_number=>$position){	

			$query = "UPDATE 

							form_elements 

						 SET 

							element_page_number ='{$page_number}'

					   WHERE

						   	form_id = '{$form_id}' and element_status=1 and element_position <= '{$position}'";

				

		        $command = $connection->createCommand($query);

	            $command->query();

		   	}

		}

		

		//update ap_forms page_total

		$query  = "update `formdetail` set form_page_total = '{$total_page}' where form_id = '{$form_id}'";

		$command = $connection->createCommand($query);

	    $command->query();

		

	}

	else if($element_type == 'matrix'){

		//delete the main element and all child rows

		$query  = "delete from `form_elements` where form_id = '{$form_id}' and element_id='{$element_id}' and element_status=2";

		$command = $connection->createCommand($query);

	    $command->query();

		

		//get all child ids and delete them from ap_element_options table

		$child_element_ids = array();

		$child_placeholders = array();

		

		$query = "select element_id from `form_elements` where form_id = '{$form_id}' and element_matrix_parent_id = '{$element_id}' and element_type='matrix' and element_status=2";

		$command = $connection->createCommand($query);

	    $form_elements=$command->queryAll();

		

		if(count($form_elements)>0){

		foreach($form_elements as $ele){

				$child_element_ids[]  = $ele->element_id;

				$child_placeholders[] = "'{$ele->element_id}'";	

			}

		}

		

		$child_element_ids[] = $element_id; //delete the first row options as well

		$child_placeholders[] = "'{$element_id}'";

		

		$child_placeholders_joined = implode(',',$child_placeholders);

		

		$query = "delete from `form_element_options` where form_id = '{$form_id}' and live = 2 and element_id in({$child_placeholders_joined})";

		$command = $connection->createCommand($query);

	    $command->query();

		

		

		//delete child rows from ap_form_elements table

		$query = "delete from `form_elements` where form_id = '{$form_id}' and element_matrix_parent_id = '{$element_id}' and element_type='matrix' and element_status=2";

		$command = $connection->createCommand($query);

	    $command->query();

		

	}else{

		$query  = "delete from `form_elements` where form_id = '{$form_id}' and element_id = '{$element_id}'";

	   	$command = $connection->createCommand($query);

	    $command->query();

	

	}

	


	$response_data = new stdClass();

	

	$response_data->status    	= "ok";

	$response_data->element_id 	= $element_id;

	

	$response_json = json_encode($response_data);

	

	echo $response_json;

	

?>



Hi!

Sorry, I’m honest:

I am simply to lazy to read through all that code and queries.

Questions:

  • Have you checked with var_dump if "form_id" is in $_POST - like I suggested above?

  • After reading your use statements…

Do you write all this queries / code in a view?

If yes => read whole yii2 guide first!

It will make your life MUCH easier…

http://www.yiiframework.com/doc-2.0/index.html

Best Regards