unWrap an element with php

Is there a way to unwrap an element other than jQuery? I’d like to unwrap the div with the ID fromWhere in the code below. I tried doing that with jQuery but it didn’t work.

    <?php  ob_start(); ?>
    
    <div id="fromWhere" >Content goes here</div>
    
    <?php $contents = ob_get_contents(div); ?>

I don’t know what you you mean by “unwrap”.
JavaScript textContent?

I want to pass the content as a variable and it doesn’t work with the DIV. If I put the content without the div, it works.

Got an existing simplified code example? (even if non-working)

It looks like below

         <?php 
                
                ob_start(); ?>
<div id="fromWhere" ></div>
<?php $contents = ob_get_contents(div); 
        $params = array(
        'origin'        =>  $contents,
        'destination'   => um_user('postal_zip_code'),
        'sensor'        => 'true',
        'units'         => 'imperial'
    );
$params_string='';
    // Join parameters into URL string
    foreach($params as $var => $val){
        $params_string .= '&' . $var . '=' . urlencode($val);  
    } ?>

I’m tired (1:30 AM here now) but my first impression is why the output buffer? In my experience this is almost always used to get around poor code. Is there a reason you need this?

Trying to use the content in an array and it won’t work being wrapped in an element.

You render div in the same script so you have full control on its content.
Place content in variable if you want to use it twice (in DIV and in array later):

<?php $contents = "Content goes here"; ?>

<div id="fromWhere"><?php echo $contents; ?></div>

<?php
    $params = array(
        'origin'        => $contents,
        'destination'   => um_user('postal_zip_code'),
        'sensor'        => 'true',
        'units'         => 'imperial'
    );

Helpful method. Mega Z. The content isn’t inserted manually. The jQuery below picks it from a profile and inserts it into #romWhere. It works only when I insert the content manually into #fromwhere. For jquery, the content shows but doesn’t work for the array. So the question is how do I get jq to get and insert the content into <?php $contents = "Content goes here"; ?>

You have to realize that jQuery (and other javascript code) runs after PHP

The scheme is:

  1. Server executes PHP code in your file and sends a result (generated HTML tags and JS code) to client (browser);
  2. Client (browser) shows received HTML tags and runs JS code.

That means your PHP and JS code work in completely different environments.

If you want send some data generated by JS (or by user) to PHP you should do AJAX-request. It’s like a running regular PHP script, but in background (no results will be shown in browser automatically).

How do I do that?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.