I have a textarea where users submit URLs to our database. The problem is that my scripts are timing out when a user submits over 1000 URLs. So I need to use some javascript and split up the data before it gets sent to the php script. I have a very basic model working outside of Yii but need some help writing the code in Yii. Any help would be appriciated.
Here's the code outside of yii
My model is just a basic gii generated model - nothing really special there
Here's my controller:
And my view (using yii-bootstrap)
This currently returns an alert for the number of URLs submitted - will want to use some form of real time feedback between calls - probably a progress bar from bootstrap.
Thanks for your help
Here's the code outside of yii
<?php if(isset($_POST['urls'])): print_r($_POST); else: ?> <script type="text/javascript" src="jquery.min.js"></script> <form action="form.php" method="post" id="target"> <textarea name="urls" id="urls" style="width:300px;height:200px">test.com test2.com test3.com test4.com test5.com test6.com test7.com test8.com test9.com test10.com test11.com test12.com test13.com test14.com test15.com test16.com test17.com test18.com test19.com test20.com </textarea> <br> <input type="submit"> </form> <script type="text/javascript"> $('#target').submit(function() { var urls = $('#urls').val().trim().split("\n"); var splitArray = chunk(urls,10); for (var ary in splitArray){ $.post('form.php',{'urls[]':splitArray[ary]}) } return false; }) function chunk (arr, len) { var chunks = [], i = 0, n = arr.length; while (i < n) { chunks.push(arr.slice(i, i += len)); } return chunks; } </script> <?php endif; ?>
My model is just a basic gii generated model - nothing really special there
Here's my controller:
public function actionAdd() { $this->seoAuthenticate(); $model=new Urls; if(isset($_POST['Urls'])): $model->attributes=$_POST['Urls']; $urls = explode("\n", $model->urlList); $success = 0; $error = 0; foreach($urls as $url) if(!empty($url)): // don't save empty lines $newUrl=new Urls; //That's what you want: an extra model for each name $newUrl->url = trim($url); $newUrl->statusID = 1; $newUrl->date = date("Y-m-d"); $newUrl->posts = 0; $newUrl->clientID = Yii::app()->user->getId(); if($newUrl->validate()): if($newUrl->save()): $success++; endif; else: $error++; endif; endif; if($success > 0) Yii::app()->user->setFlash('success', $success . ' URLs added.'); if($error > 0) Yii::app()->user->setFlash('error', $error . ' duplicate or malformed URLs were not added.'); $this->redirect('/urls/new/'); /*$this->render('add',array( 'model'=>$model, ));*/ else: $this->render('add',array( 'model'=>$model, )); endif; }
And my view (using yii-bootstrap)
<?php /* @var $this UrlsController */ /* @var $model Urls */ ?> <div class="span12" style="margin: 50px 0 10px"><?php $this->widget('ext.widgets.controlBar');?> </div> <h1 class="title">Add Urls</h1> <?php $this->widget('bootstrap.widgets.TbAlert', array( 'block'=>true, // display a larger alert block? 'fade'=>true, // use transitions? 'closeText'=>'×', // close link text - if set to false, no close link is displayed 'alerts'=>array( // configurations per alert type 'success'=>array('block'=>true, 'fade'=>true, 'closeText'=>'×'), // success, info, warning, error or danger ), )); ?> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'urls-form', 'enableAjaxValidation'=>false, 'htmlOptions'=>array( 'onsubmit'=>'return false', ), )); ?> <p class="note">Add 1 URL per line</p> <?php echo $form->errorSummary($model); ?> <div class="row" style="margin-left:0"> <?php echo $form->labelEx($model,'urlList'); ?> <?php echo $form->textArea($model,'urlList',array('style' => 'width:90%;height:500px')); ?> <?php echo $form->error($model,'urlList'); ?> </div> <div class="row buttons" style="margin-left:0"> <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'label'=>'Submit')); ?> <?php //echo CHtml::submitButton('Save'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->
This currently returns an alert for the number of URLs submitted - will want to use some form of real time feedback between calls - probably a progress bar from bootstrap.
Thanks for your help