开发者

Parsing URL jQuery AJAX form

开发者 https://www.devze.com 2023-03-09 19:59 出处:网络
Basically i\'m trying to send a video along with other info through jQuery to PHP to be written to a txt file to be read later.

Basically i'm trying to send a video along with other info through jQuery to PHP to be written to a txt file to be read later.

There is a way of inputting a video url into this. I've got everything working except one thing.

If i put this through: http://www.youtube.com/watch?v=g1lBwbhlPtM it works fine.

but this: http://www.youtube.com/watch?v=g1lBwbhlPtM&featur开发者_开发知识库e=feedu doesn't.

I've done some tests and it's because when i send the second url through &feature=feedu gets read as a separate $_POST value.

This is the problem:

    var dataString = 'title='+title+'&content='+content+'&date='+date+'&Submit=YES'; 

because its reading like

var dataString = 'title='+title+'&content='+IMAGES, TEXT AND STUFF+'&feature=feedu OTHER IMAGES AND STUFF&date='+date+'&Submit=YES';

it's out of a textarea that could include images or text and stuff so im looking for something like htmlspecialchars() to sort out that & before sending it through ajax

Any ideas how to solve this?

EDIT: Here's the full code that's the problem:

    var title = $('input#title').val(); 
    var content = $('textarea#content').val();
    var date = $('input#date').val();
    var dataString = 'title='+title+'&content='+content+'&date='+date+'&Submit=YES'; 
    //alert (dataString);return false;
    $.ajax({
     type: "POST",
     url: "./inc/php/file.php",
     dataType: "json",
     data: dataString,
     success: function(data) {
        if(data.error == true){
             $('.errordiv').show().html(data.message);
        }else{
             $('.errordiv').show().html(data.message);
             $(':input','#addstuff')
                .not(':button, :submit, :reset, :hidden')
                .val('')
                .removeAttr('checked')
                .removeAttr('selected');
        }
       },
       error: function(data) {
        $('.errordiv').html(data.message+' --- SCRIPT ERROR');
       }

    })
    return false;

if content equals:

&content= <br>Text 1<br> <img>http://someimage.com/image.jpg</img> 
<br> Text2<br> <vid>http://www.youtube.com/watch?v=isDIHIHI&feature=feedu</vid> 
<br>Text 3<br>

the content variable gets put through the ajax call as:

&content= <br>Text 1<br> <img>http://someimage.com/image.jpg</img> 
<br> Text2<br> <vid>http://www.youtube.com/watch?v=isDIHIHI

with an extra variable that is

&feature=feedu</vid> 
<br>Text 3<br>

So how do u stop the ajax reading &feature as a separate $_POST variable?


Did you encodeURI() before pass the your video url?

If you need it in PHP then URLEncode


I used this bit of code in the php file

if(isset($_POST['feature'])){
    $content=htmlspecialchars_decode(stripslashes(nl2br("<br />".$_POST['content'].'&feature='.$_POST['feature']."<br />")));
}

But it's not very dynamic as it only applies to youtube URLs


In JS do (before ajaxing)

dataString = encodeURI(dataString);

And then decode it on PHP

$dataString = urldecode($_POST['data']);

Or do:

 $.ajax({
     type: "POST",
     url: "./inc/php/file.php",
     dataType: "json",
     data: {
       'title': $('input#title').val(),
       'content': $('textarea#content').val(),
       'date': $('input#date').val(),
       'Submit': 'Yes'
     }
     success: function(data) {
        if(data.error == true){
             $('.errordiv').show().html(data.message);
        }else{
             $('.errordiv').show().html(data.message);
             $(':input','#addstuff')
                .not(':button, :submit, :reset, :hidden')
                .val('')
                .removeAttr('checked')
                .removeAttr('selected');
        }
       },
       error: function(data) {
        $('.errordiv').html(data.message+' --- SCRIPT ERROR');
       }

    })
0

精彩评论

暂无评论...
验证码 换一张
取 消