Ahsan Mohyuddin
Ahsan Mohyuddin

Reputation: 181

Staying on the same page location after form submit without ajax in php

I am using form post method to upload images.

$form = $this->beginWidget(
            'CActiveForm',
             array(
                'id' => 'upload-form',
                'enableAjaxValidation' => false,
                'action'=>Yii::app()->createUrl('/admin/user/uploadimage'),
                'htmlOptions' => array('enctype' => 'multipart/form-data')
             ));

            echo $form->fileField($model, 'image');
            echo CHtml::submitButton('Upload');
            $this->endWidget();

When images are uploaded, the page gets refreshed and top of the page appears. Is there is any possibility restrict the page on the same location in the view when form is submited, with out using ajax. Please help me or make some suggestions to achieve the scenario. Or it is not possible with form post.

Upvotes: 0

Views: 913

Answers (4)

Ahsan Mohyuddin
Ahsan Mohyuddin

Reputation: 181

The easiest way that i found is:-

In view.php assign id to html div tag, like i have assigned "image-section"

<div id ="image-section"
    <?php $form = $this->beginWidget(
        'CActiveForm',
         array(
            'id' => 'upload-form',
            'enableAjaxValidation' => false,
            'action'=>Yii::app()->createUrl('/admin/user/uploadimage'),
            'htmlOptions' => array('enctype' => 'multipart/form-data')
         ));

        echo $form->fileField($model, 'image');
        echo CHtml::submitButton('Upload');
        $this->endWidget();?>
</div>

And now in controller append #id along with url.

public function actionUploadImage()
{
//You code
 $scrollLocation = "#image-section"
 $this->redirect ( "index$scrollLocation", array (
            'model' => $model 
    ) );
} 

Upvotes: 1

acorncom
acorncom

Reputation: 5955

The short answer is No.

The long answer is maybe :-) Without Ajax, you will need to do a full page refresh, although you can send a user down to the image upload section. But if you don't want the page to refresh, tou will need to use Ajax.

Upvotes: 1

Mudassir
Mudassir

Reputation: 596

Is your image form showing after submit if yes then add this code at the bottom of your page: if not then assign an id to any div and change upload-form to that in the below code

<?php
   if(isset($_FILES["image"]["type"])){
?>
<script type="text/javascript">
   window.location='#upload-form';
</script>
<?php   
}?>

Upvotes: 3

Jafer.balti
Jafer.balti

Reputation: 584

You can also use CURL if you don't want to use ajax. Here is the link to follow :)

http://php.net/manual/en/book.curl.php

Upvotes: 3

Related Questions